bit-logic

Search IconIcon to open search

Source: samek-embedded

LogicDescriptionNotes
(num & 1) != 0Odd numberTests the least significant bit
(num & 1) == 0Even number
a | bBit-wise OR
a & bBit-wise AND
a ^ bExclusive OR
~bNOT

Bit shift operations

Unsigned

Uses logical bit shifting.

Define an unsigned number:

1
u =  101 = 0x65 = 0b0110'0101

Right shift (unsigned)

Corresponds to integer division by $2^n$

1
2
c = u >> 1
c = 0b0011'0010 = 50

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’.

1
2
c = u << 1
c = 0b1100'1010 = 202
1
2
c = u << 3
c = 0b0010'1000 = 40

In assembly: LSLS shifts zeroes into the least significant bit positions.

Signed

Define an unsigned and a signed number:

1
2
u =  101 = 0x65 = 0b0110'0101
s = -101 = 0x9B = 0b1001'1011

Right shift (signed)

Corresponds to division by $2^n$, rounded up if negative, rounded down in positive.

1
2
c = u >> 3
c = 0b0000'1100 = 12 = floor(101 / 8)
1
2
c = s >> 3
c = 0b1111'0011 = 243 = -13 = - ceil(101 / 8)

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

1
value |= mask_of_bit;

Bit clear

1
value &= ~mask_of_bit;