Bitwise integer operations
Last updated: Feb 11, 2025
These functions enable integers to be manipulated as bit patterns representing
two's-complement values, where bit position
has weight
N
.2**N
Bits are numbered from 0 upward. These operations act as though the sign bit of an integer is extended indefinitely to the left. Thus, everywhere above its most significant bit, a positive integer has 0 bits and a negative integer has 1 bit.
Function | Result | Description |
---|---|---|
|
Integer | Produces the bitwise complement of the integer INT1. That is, there is a 1 in the
result for each bit position for which INT1 has 0. It is always true that . |
|
Integer | The result of this operation is the bitwise "inclusive or" of INT1 and INT2. That is, there is a 1 in the result for each bit position for which there is a 1 in either INT1 or INT2 or both. |
|
Integer | The result of this operation is the bitwise "exclusive or" of INT1 and INT2. That is, there is a 1 in the result for each bit position for which there is a 1 in either INT1 or INT2 but not in both. |
|
Integer | Produces the bitwise "and" of the integers INT1 and INT2. That is, there is a 1 in the result for each bit position for which there is a 1 in both INT1 and INT2. |
|
Integer | Produces the bitwise "and" of INT1 and the bitwise complement of INT2. That is,
there is a 1 in the result for each bit position for which there is a 1 in INT1 and a 0 in
INT2. This is the same as
and is useful for clearing bits of INT1 set in
INT2. |
|
Integer | Produces the bit pattern of INT1 shifted left by N positions. A negative value for N produces a right shift. |
|
Integer | Produces the bit pattern of INT1 shifted right by N positions. A negative value for N produces a left shift. |
|
Boolean | Equivalent to the Boolean expression but is more
efficient. |
|
Boolean | Equivalent to the Boolean expression but is more
efficient. |
|
Integer | Counts the number of 1 or 0 bits in the two's-complement representation of INT. If
INT is non-negative, N is the number of 1 bits. If INT is negative, it is the
number of 0 bits. Owing to the sign extension, there are an infinite number of 0 bits in a
non-negative integer or 1 bits in a negative integer. It is always the case that
. |
|
Integer | Returns the bit position N of the least-significant bit set in the integer INT. N is the highest power of 2 by which INT divides exactly. |
|
Integer | Returns the length in bits of INT as a two's-complement integer. That is, N is
the smallest integer such that . If INT is non-negative, then the representation of INT as
an unsigned integer requires a field of at least N bits. Alternatively, a minimum of
N+1 bits is required to represent INT as a signed integer, regardless of its
sign. |
|
Boolean | Tests the bit at position N in the integer INT and returns the state of bit N as a Boolean value, which is true for 1 and false for 0. |
Was the topic helpful?
0/1000