BIT SHIFTING
IsPalindrome
public static bool IsPalindrome(int n) { for (int i = 0; i < 16; i++) { if (((n >> i) & 1) != ((n >> (31 - i)) & 1)) { return false; } } return true; }
(i & 1)=(i % 2 == 1)
(i % 2 == 1)
x << y A left shift of n bits multiplies by 2 raise to n.
-5 >> 3 is -1 and not 0 like -5/8.
Reverses bits in a byte PORTABLE CODE unsigned char rev( unsigned char b ) { unsigned char result = 0; while (b) { result <<= 1; result |= b % 2; b >>= 1; } return result } C CODE
// Reverses bits in a byte static byte Reverse(byte b) { int rev = (b >> 4) | ((b & 0xf) << 4); rev = ((rev & 0xcc) >> 2) | ((rev & 0x33) << 2); rev = ((rev & 0xaa) >> 1) | ((rev & 0x55) << 1); return (byte)rev; }
nearest power of two greater or equal to given number: 1 << (int)(ceil(log2(given)))
1 << (int)(ceil(log2(given)))
adds two numbers You should not use + or any arithmetic operators
1 int add_no_arithm(int a, int b) { 2 if (b == 0) return a; 3 int sum = a ^ b; // add without carrying 4 int carry = (a & b) << 1; // carry, but don't add 5 return add_no_arithm(sum, carry); // recurse 6 }
get Max with out if else
1 int getMax(int a, int b) { 2 int c = a - b; 3 int k = (c >> 31) & 0x1; 4 int max = a - k * c; 5 return max; 6 }
Unique Characters in string
@can= checker & (1 << val) @cor= checker | (1 << val); Unique Characters in string for (int i = 0; i < str.length(); ++i) { int val = str.charAt(i) - 'a'; if (@can) > 0) return false; checker = @cor }
SUM CARRY
SUM = A XOR B CARRY = A AND B
How to reverse the odd bits of an integer?
XOR each of its 4 hex parts with 0101.
GetPixel (bool)((c >> (((y * width) + x) % 8) & 1));
bool pBuffer::GetPixel(unsigned int x, unsigned int y) char c = blocks[((y * width) + x) / 8]; return (bool)((c >> (((y * width) + x) % 8) & 1));
( ( $row_count & 1 ) == 0 ) ? EVEN: ODD; ...
checking for a 0 in the last column and that's a much quicker way of figuring out if the row is even or odd. // Our Bitwise & to make Black or White squares var addSquare:Boolean = ( (counter & 1) == 0 ) ? fill( square, 0xFFFFFF ) : fill( square, 0x000000 );
Determine how many bits are "on" in a given integer:
function howManyBits(num) { var numBits = 0, i = 1, theAnd = Math.pow(2, i); do { numBits += (num & theAnd)?1:0; theAnd = Math.pow(2, ++i) } while (theAnd <= num); return numBits; }
Print all the odd numbers from 1 to N:
if (i & 1) theString += pad(i.toString(),3) function print_odds() { var theString = ""; for (var i = 0; i < 100; i++) { if (i & 1) { theString += pad(i.toString(), 3); } } return theString; }
// Power of 2!
if(!(num & (num - 1)) && num) { // Power of 2! } Method2 if(((~i+1)&i)==i) { //Power of 2! }
Check Set Bit
int CheckSetBit(int num, int pos) { int x = 1; x = ~(x<<pos); // creating mask in which only pos bit is unset return ((num & x) == num); // checking whether number is same... if same then pos bit is set }
Little-Endian Big-Endian checks whether the 1 is stored in the first byte (that's what the char cast does) of the int or not
int num = 1; if(*(char *)&num == 1) { printf("\nLittle-Endian\n"); } else { printf("Big-Endian\n"); }
numOnesInBinary n&1=1?count++
int numOnesInBinary( int number ) { int numOnes = 0; while( number != 0 ){ if( ( number & 1 ) == 1 ) numOnes++; number = number >>> 1; } return numOnes; }
// Reverses bits in each byte in the array
static void Reverse(byte[] bytes) { // Precompute the value of each reversed byte byte[] reversed = new byte[256]; for (int i = 0; i < 256; i++) reversed[i] = Reverse((byte)i); // Reverse each byte in the input for (int i = 0; i < bytes.Length; i++) bytes[i] = reversed[bytes[i]]; }
swap with out temp
swap(int *a, int *b) { *a ^= *b ^= *a ^= *b; }
turn on the bit 3 B := B XOR 4;
turn on the bit 3 (right to left) of a byte
swap nibbles
unsigned char swap_nibbles(unsigned char c) { unsigned char temp1, temp2; temp1 = c & 0x0F; temp2 = c & 0xF0; temp1=temp1 << 4; temp2=temp2 >> 4; return(temp2|temp1); //adding the bits }
reverse the bits in an integer temp = temp | (num & 1);
unsigned int num; // Reverse the bits in this number. unsigned int temp = num; // temp will have the reversed bits of num. int i; for (i = (sizeof(num)*8-1); i; i--) { temp = temp | (num & 1); temp <<= 1; num >>= 1; } temp = temp | (num & 1);
Count Set Bits n &= (n-1);
unsigned int v; // count the number of bits set in v unsigned int c; // c accumulates the total bits set in v for (c = 0; v; v >>= 1) { c += v & 1; }