bit-algo
Bit Algorithms#
The sign bit is 1 in negative numbers, and 0 in positive numbers. The XOR of x and y will have the sign bit as 1 iff they have opposite sign. In other words, XOR of x and y will be negative number number iff x and y have opposite signs.
taking && or OR with 1 to get the info on what type of bit we have
const result = 5 & 3; // Binary: 0101 & 0011 = 0001console.log(result); // Output: 1const result = 5 | 3; // Binary: 0101 | 0011 = 0111console.log(result); // Output: 7- use left shift and right shift to manipulate bits e.g
for a 32 bit integer you can do lets say 1 << 31 : bit shifted by 31 places
a >> 1b << 1- XOR stores the 2 numbers data in a single number and later it can be used to extract the 2 numbers. e.g let a = b^c; // a stores the xor of b and c b = a^c; // b can be extracted c = a^b;