Profile

Unblock all features

Sign in to track progress and visualise your code.

ntree.ai
Fundamentals

Bit manipulation

Bit manipulation tricks can sometimes optimise your solution with a small number of operations and compact code. They appear in fewer problems than arrays or hash tables, but a few operations cover most of them.

Binary representation

In binary representation, the only possible digits are 0 and 1, and the scale factor is 2 instead of 10. For example, 110 in binary is 6 in decimal.

1
1
0
421
1 * 4 + 1 * 2 + 0 * 1 = 6

Binary to decimal

To convert a binary number to decimal, we multiply each digit by its scale and add the results. The scale starts at 1 for the last digit and doubles with every step to the left.

1
1
0
1
8421
scale = 1, 1 * 1 = 1, total = 1
1 / 5
To decimal
Python
def to_decimal(bits: str) -> int:
# Return the number that the binary digits stand for
return 0
print(to_decimal('1101'))

Decimal to binary

To convert a decimal number to binary, we divide it by 2. The last bit of n is n % 2, and dividing n by 2 drops it. Repeat until n is 0, and the bits come out from right to left.

13
1
13 % 2 = 1, 13 / 2 = 6
1 / 5
To binary
Python
def to_binary(n: int) -> str:
# Return the binary digits of n as a string, using only % 2 and // 2
return ''
print(to_binary(13))

Bitwise operations

AND (&)

Returns 1 only when both bits are 1.

1
1
0
0
1
0
1
0
202
&
1
0
1
0
1
1
0
0
172
1
0
0
0
1
0
0
0
136
11001010 & 10101100 = 10001000

OR (|)

Returns 1 when at least one of the bits is 1.

1
1
0
0
1
0
1
0
202
|
1
0
1
0
1
1
0
0
172
1
1
1
0
1
1
1
0
238
11001010 | 10101100 = 11101110

XOR (^)

Returns 1 when the bits are different.

1
1
0
0
1
0
1
0
202
^
1
0
1
0
1
1
0
0
172
0
1
1
0
0
1
1
0
102
11001010 ^ 10101100 = 01100110

NOT (~)

Flips every bit.

1
1
0
0
1
0
1
0
202
~
0
0
1
1
0
1
0
1
53
~11001010 = 00110101

Left shift (<<)

Moves every bit k places to the left and fills the right with zeros.

0
0
1
0
1
1
0
1
45
<< 2
1
0
1
1
0
1
0
0
180
00101101 << 2 = 10110100

Right shift (>>)

Moves every bit k places to the right and drops the last k bits.

0
0
1
0
1
1
0
1
45
>> 2
0
0
0
0
1
0
1
1
11
00101101 >> 2 = 00001011

Tricks

Check the last bit

n & 1 returns the last bit of n, because 1 is 00000001 and AND with 0 turns every other bit into 0.

1
0
1
1
0
1
0
1
181
&
0
0
0
0
0
0
0
1
1
0
0
0
0
0
0
0
1
1
10110101 & 1 = 1

Check bit k

(n >> k) & 1 returns bit k of n: the shift moves it to the last place, and & 1 keeps only that bit.

1
0
1
1
0
1
0
1
181
>> 2
0
0
1
0
1
1
0
1
45
&
0
0
0
0
0
0
0
1
1
0
0
0
0
0
0
0
1
1
(10110101 >> 2) & 1 = 1

Set bit k

n | (1 << k) sets bit k to 1 and keeps the other bits, because 1 << k has a 1 only at bit k, and OR with 1 gives 1 while OR with 0 keeps the bit.

0
0
0
0
0
0
0
1
1
<< 3
0
0
0
0
1
0
0
0
8
|
1
0
1
1
0
1
0
1
181
1
0
1
1
1
1
0
1
189
10110101 | (1 << 3) = 10111101

Clear bit k

n & ~(1 << k) sets bit k to 0 and keeps the other bits, because ~(1 << k) has a 0 only at bit k, and AND with 0 gives 0 while AND with 1 keeps the bit.

0
0
0
0
0
0
0
1
1
<< 2
0
0
0
0
0
1
0
0
4
~
1
1
1
1
1
0
1
1
251
&
1
0
1
1
0
1
0
1
181
1
0
1
1
0
0
0
1
177
10110101 & ~(1 << 2) = 10110001

Toggle bit k

n ^ (1 << k) flips bit k and keeps the other bits, because 1 << k has a 1 only at bit k, and XOR with 1 flips the bit while XOR with 0 keeps it.

0
0
0
0
0
0
0
1
1
<< 4
0
0
0
1
0
0
0
0
16
^
1
0
1
1
0
1
0
1
181
1
0
1
0
0
1
0
1
165
10110101 ^ (1 << 4) = 10100101

Remove the last 1 bit

n & (n - 1) removes the last 1 bit of n, because n - 1 turns that bit into 0 and every 0 after it into 1.

1
0
1
1
0
1
0
0
180
- 1
1
0
1
1
0
0
1
1
179
&
1
0
1
1
0
1
0
0
180
1
0
1
1
0
0
0
0
176
10110100 & (10110100 - 1) = 10110000
Was this lesson helpful?