MyReads Flashcards
Bitwise XOR (^)
It returns bit by bit XOR of input values, i.e, if corresponding bits are different, it gives 1, else it gives 0.
2’s complement
- invert all bits and add 1 bit
byte short int long float double boolean char
byte: Byte data type is an 8-bit signed two’s complement integer.
Short: Short data type is a 16-bit signed two’s complement integer.
int: Int data type is a 32-bit signed two’s complement integer.
long: Long data type is a 64-bit signed two’s complement integer.
float: Float data type is a single-precision 32-bit IEEE 754 floating point.
double: double data type is a double-precision 64-bit IEEE 754 floating point.
boolean: boolean data type represents one bit of information.
char: char data type is a single 16-bit Unicode character.
Bitwise OR (|)
It returns bit by bit OR of input values, i.e, if either of the bits is 1, it gives 1, else it gives 0.
Bitwise AND (&)
It returns bit by bit AND of input values, i.e, if both bits are 1, it gives 1, else it gives 0.
a = 0011 1100 b = 0000 1101 a&b a|b a^b ~a
a = 0011 1100
b = 0000 1101
—————–
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011
Remove last bit ?
Remove last bit A&(A-1)
Sum of Two Integers
int getSum(int a, int b) { return b==0? a:getSum(a^b, (a&b)<<1); //be careful about the terminating condition; }
Missing Number:
Given an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is missing from the array
Loop:
ret ^= i;
ret ^= nums[i];
return ret^=nums.size();
Count 1 bits
Integer.bitCount();
x%2==1 and x = x»_space;1
do until 0 - x & (x-1)
Turn off only most significant bit
do -1
Retrieve the Right most bit
%2 or &1