bit-logic
Source: samek-embedded
Logic | Description | Notes |
---|---|---|
(num & 1) != 0 | Odd number | Tests the least significant bit |
(num & 1) == 0 | Even number | |
a | b | Bit-wise OR | |
a & b | Bit-wise AND | |
a ^ b | Exclusive OR | |
~b | NOT |
Bit shift operations
Unsigned
Uses logical bit shifting.
Define an unsigned number:
|
|
Right shift (unsigned)
Corresponds to integer division by $2^n$
|
|
In assembly: LSRS
shifts zeroes into the most significant bit positions.
Left shift (unsigned)
Corresponds to multiplication by $2^n$, but taking into account the most significant bits that ‘fall off the edge’.
|
|
|
|
In assembly: LSLS
shifts zeroes into the least significant bit positions.
Signed
Define an unsigned and a signed number:
|
|
Right shift (signed)
Corresponds to division by $2^n$, rounded up if negative, rounded down in positive.
|
|
|
|
In assembly: ASRS
(arithmetic right shift) shifts zeroes or ones into the most significant bit positions, depending on most significant bit. This preserves the sign.
Bit setting and clearing
Change [a] specific bit/s without affecting the other bits.
Bit set ORR.W
|
|
Bit clear
|
|