Bitwise operator
&: if a&1 then last digit of a will always same as in previous a.
a^1= compliment (a),
a^0=a, (diff bit =a)
a^a=0, (same bit =0)
coversion :
Decimal --> n base = keep divide by base
exp : (17)D = ()2 = .......
n base --> decimal = multiply each digit with the power of base and add them.
exp: 1* 2^3 + 0*2^2 + 0 * 2*1+ 1*2^0=..
Left shift : a<<b = a* (2^b)
Right shift : a>>b = a/(2^b)
unique no. in duplicate array : xor all elements
find ith bit of a no. n = n & (1<<n-1)
set ith bit = n | (1<<n-1)
reset ith bit= !(1<<n-1)
Find position of rightmost set bit
negative no. in binary= 1st bit (MSB) = if 0 (+ve), id 1(-ve)
neg no. by 2's compliment : find compliment of no. ..then add +1
1 byte range = -128 to 127 (total 256 no. can be stored..2^7) because we ignore 1st bit
formula of range = -2^(n-1) to 2^(n-1) -1
find uniq no. in odd repeating no. array
find nth magic no.
find no. of digit in no. n havining base b= (int) (log(n)/log(b)) +1
check no. is a power of 2 : n &( n-1) ==0
find the xor of 0 to a =
- a%4==0, ans= a
- a%4 ==1, ans= 1
- a%4==2,ans=a+1
- a%4==3,ans=0
- total no of digit -1 of int : digit =(int)log10(a)
- 1st digit of number : (int)(a/pow(10,digit));
Comments
Post a Comment