Dhruv_gheewala's blog

By Dhruv_gheewala, history, 3 weeks ago, In English,

Can anyone please help me. What will be the answer of let's say x is a number then what will be answer of (-x & x). please tell i know that if +x's binary is 0111 then -x's binary will be 1111 and their bitwise and will be +x but i am getting different answer.please help me.

And thanks in advance.

 
 
 
 
  • Vote: I like it
  • 0
  • Vote: I do not like it

»
3 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Refer to this link : Tutorial

»
3 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Bro (x & -x) just withdraws the last set bit of x;

For example x = 0110

So x&-x = 0010

»
3 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Say the first 1 is at the ith position from LSB side of binary representation of x, then the very expression will be equal to 2^(i-1) where i is based on 1 indexing.

»
3 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

if x is 0111 then its 1's complement is 1000. After adding 1 to 1's complement you will get 1001. So -x is 1001 and doing & operation with x it will give 0001. So expression (x & -x) gives first location of 1 in x from LSB side.