Posts

Showing posts from June, 2018

Bit Operation

Bit Operation Essential Things For details about Bit,  this  blog is helpful. For my revise :) Left Shift ( << ) means multiply a number by 2, or, some powers of 2. Approach : a<<2   where a = 1011101 after operation                 a = 1110100 Right Shift ( >> ) means divide a number by 2 or powers of 2. Approach : a>>2   where a = 1011101 after operation                 a = 0010111 Precedence of Bit Operator NOT ( ~ ) highest AND ( & ) XOR ( ^ ) OR ( | ) lowest Is a power of 2? bool isPowerOfTwo ( int x ) { // x will check if x == 0 // and !(x & (x - 1)) will check if x is a power of 2 or not return ( x && !( x & ( x - 1 ))); } Count the number of ones in the binary representation of the given number.. int count_one ( int n ) { while ( n ) { n = n &( n - 1 ); count ++; } return