Bitwise operation Flashcards
What is XOR of a number to itself
0
How to use bitwise operator to check if number is even or odd?
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
Which bitwise operator to use to multiply number by power of 2
left shift operator 10«2 will give 10*4(2^2)=40 as a result
Which bitwise operator to divide the number by power of 2?
right shift operator 20»2 will give 20/(2^2)=20/4=5 as a result
How to unset the right most set bit of a number?
https://www.techiedelight.com/bit-hacks-part-3-playing-rightmost-set-bit-number/
n & n-1
If number of 1-bits in 2 are 1 , how many number of bits, 4 will have ?
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 to know if last bit of a given number is 0 or 1
num & 1 =0 last bit is zero , otherwise less bit is one
How to convert int in base 10 to base 2 string?
int base2=2;
Convert.ToString(123,base2); will output 1111011
Give a string containing base 2 number(101), how can we convert it into base 10 number
int number=Convert.ToInt32(str,2);