Bit Manipulation Flashcards
How do you perform addition
1101 + 1010
Answer: 1111
Explanation:
1+ 0 /0 +1 = 1
0+0 = 0
1+1 = 0 with 1 as carry
How do you perform subtraction
1101 + 1010
Answer: 0011
Explanation:
0-1 . In this case you need to borrow 1 from the 1 at position 2 thus making it 0 and making the value at position 1 as 2 and 2-1 =1
What is 1101 «_space;2
0111 . you add 1’s when you move bits
What happens when you do XOR of two numbers?
only when both bits are not equal 1 is returned
What is 2’s complement?
flip the bits of a number and add 1 to it
How do you perform Multiplication?
1101 * 0100.
1101 * 0, 11010, 11011, 1101*0 and add all of them
How do you get the i th bit?
use mask =1 «_space;i. Shift the value 00000001 to the left by i bits so the 1 bit will be at i position.
then you can do num & mask. This will ensure you only get the bit at i position
How do you set the i th bit?
use mask = 1 «_space;i. Shift the value 00000001 to the left by i bits so the 1 bit will be at i position.
then you can do num | mask. This will ensure you only set the bit at i position