c - Explanation of Output of Bitwise operations -


this question has answer here:

void main()  {  int x=7;  printf("%d",x&(x-1)); int y=6;  printf("%d",y&(y-1));  printf("%d",y>>2);    } 

when put odd number output n-1 n odd number when put y= number output 0.i not able understand please help.

my second question when print y>>2 6>>2 ouput 1.please explain me also. know these bitwise operations concept not clear.thanks

let's break each line up:

x&(x-1) => 0x111 & 0x110 => 0x110 => 6 

... and:

y&(y-1)) => ox110 & 0x101 => 0x100 => 4 

... , finally:

y>>2 => 0x110 >> 2 => 0x001 => 1 

remark: it's idea review knowledge of bitwise operations.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -