bit-wise manipulation Flashcards

1
Q

x^0s ^ = XOR

A

x XOR with 0s leave 0s equal to zero and ones equal to one

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

x^1s

A

NOT X

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

x^x

A

0s

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

x & 0s

A

0s

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

x & 1s

A

X

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

x & x

A

x

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

x | 0s

A

x

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

x | 1s

A

1s

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

x | x

A

x

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

What is the difference between the arithmetic shift &laquo_space;and the logic shift «

A

they are similar, they shift to the left or right the bits. The arithmetic one though keeps the same most significant digits, the logic one substitute that with zeros.

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

Get Bit i operation?

A

create a mask 1«i></i> then apply AND with number and see if the results is the zero or not

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

clear bits

A

mask of 11110111 with not i«i></i> then you apply an end

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

clever way to create a mask 000011111

A

generates a mask 000010000 with 1«i></i> then substract one

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

set a bit equal to one

A

simply use the mask 00010000 with i«i></i>

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

set a bit to some input value

A

You first clear by doing a non(i«i></i>) so you and this with the input then if v is your input value you or the results with v«i></i>

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

How do we represent a negative number in bynary?

A

With the 2 complements. i.e we now start with 1 the sign bit. Then we use the two complement. i.e the number you have to add to the input to get to 2^bits where bits is the nu,ber of bits. You can get that by starting with the complement (0 where number is 1 and viceversa) and then add 1 to that.

17
Q

Check if number is even?

A

(num & 1) ==0

18
Q

check if number power of 2?

A

(num & num -1) == 0

19
Q

How do you check if bit array is composed by only one 1 and all zeros?

A

You can do (num - 1) and (num) ==0

0010000 –> 0001111 so they do not overlap

20
Q

How do you set the rightmost 1 bit to zero?

A

By doing x & (x-1)