Bits Manipulation Flashcards

1
Q

How to remove the lowest set bit in x?

How to extract the lowest set bit in x?

A

How to remove the lowest set bit in x?

x & (x - 1)

How to extract the lowest set bit in x?

x & ~(x - 1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

print(bin(4))

A

0b100
2 ** 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

print(bin(8))

A

0b1 000
2 ** 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

print(bin(16))

A

0b10 000
2 ** 4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

print(bin(32))

A

0b100 000
2 ** 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

print(bin(64))

A

0b1000 000
2 ** 6

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

print(bin(128))

A

0b10 000 000
2 ** 7

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

print(bin(256))

A

0b100 000 000
2 ** 8

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

print(bin(512))

A

0b1 000 000 000
2 ** 9

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

print(bin(1024))

A

0b10 000 000 000
2 ** 10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

2 ** 5

A

32

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

2 ** 6

A

64

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

2 ** 7

A

128

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

2 ** 8

A

256

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

2 ** 9

A

512

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

hex_num = “0xF”
decimal_num = int(hex_num, 16)
binary_num = bin(decimal_num)
print(binary_num)

What does this print out?

A

print(binary_num) # Output: ‘0b1111’

17
Q

num = hex(0b111)
print(num)

18
Q

num = hex(0b11)
print(num)

19
Q

num = hex(0b1111)
print(num)

20
Q

num = hex(0b11111)
print(num)

A

‘0x1f’

21
Q

decimal = -1
What’s the 8-bit Two’s Complement of decimal?

A

8-bit Two’s Complement of -1 = 1111 1111

8-bit Two’s Complement of 1 = 0000 0001

只有2的几次方的数 正数和负数的significant digit都一样 其他digit 都相反

22
Q

decimal = -2
What’s the 8-bit Two’s Complement of decimal?

A

8-bit Two’s Complement of -2 = 1111 1110

8-bit Two’s Complement of 2 = 0000 0010

只有2的几次方的数 正数和负数的significant digit都一样 其他digit 都相反

23
Q

decimal = -4
What’s the 8-bit Two’s Complement of decimal?

A

8-bit Two’s Complement of -4 = 1111 1100

8-bit Two’s Complement of 4 = 0000 0100

只有2的几次方的数 正数和负数的significant digit都一样 其他digit 都相反

24
Q

decimal = -128
What’s the 8-bit Two’s Complement of decimal?

A

8-bit Two’s Complement of -128 = 1000 0000

8-bit Two’s Complement of 128= 1000 0000

只有2的几次方的数 正数和负数的significant digit都一样 其他digit 都相反

25
Q

current_bit &1
current_bit &0
current_bit ^ 1
current_bit ^ 0

What do these do to the current bit?

A

current_bit &1 = current bit stays the same
current_bit &0 = current bit gets erased
current_bit ^ 1 = flips the current bit
current_bit ^ 0 = current bit stays the same

26
Q

What happens with XOR?
a ^ b = c

a, b are binary bits

A

c = 0 when a == b
c = 1 when a != b

27
Q

With 2’s complement, -x = ________ ?

A

-x = ~x + 1
~x = -x - 1

28
Q

With 2’s complement, ~x is ?

A

~x = -x -1

29
Q

We have an integer x,
how can we convert it to -x using binary operations?

A
  1. subtract 1 from x
  2. flip each bit of 1

x = 0001
x - 1 = 0000
flipped x - 1 =1111 = -1