Bitwise operation Flashcards

1
Q

What is XOR of a number to itself

A

0

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

How to use bitwise operator to check if number is even or odd?

A
If number is even, its least significant bit will be 0. For odd , its least significant bit will be 1
if ( (num & 1 )  == 1 ) 
odd number 
else 
even number
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Which bitwise operator to use to multiply number by power of 2

A

left shift operator 10«2 will give 10*4(2^2)=40 as a result

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

Which bitwise operator to divide the number by power of 2?

A

right shift operator 20»2 will give 20/(2^2)=20/4=5 as a result

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

How to unset the right most set bit of a number?

A

https://www.techiedelight.com/bit-hacks-part-3-playing-rightmost-set-bit-number/
n & n-1

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

If number of 1-bits in 2 are 1 , how many number of bits, 4 will have ?

A

Number of bits in even number are equal to how many number of bits in its half like n/2.
In case of odd number , add 1 i.e. n/2+1

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

How to know if last bit of a given number is 0 or 1

A

num & 1 =0 last bit is zero , otherwise less bit is one

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

How to convert int in base 10 to base 2 string?

A

int base2=2;

Convert.ToString(123,base2); will output 1111011

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

Give a string containing base 2 number(101), how can we convert it into base 10 number

A

int number=Convert.ToInt32(str,2);

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