• Post Reply Bookmark Topic Watch Topic
  • New Topic

Bit Manipulation help needed  RSS feed

 
M Hasan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am reading a book and came across a code which I don't get. Please help:


My understanding is display mask = -2147483648 which is    10000000 00000000 00000000 00000000
and for loop 32th iteration input is = -2147483648 which is 10000000 00000000 00000000 00000000
Then the AND has to be   ------------------------------------------- 10000000 00000000 00000000 00000000

But the code output is 00000000 00000000 00000000 00000001

Please clarify if someone out there knows how it is working. Please also give me some example if it is possible like how input -1 will behave.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Your code is going from right to left ... so you are getting the expected result, except, you are printing the result from right to left.

Henry
 
M Hasan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so what is the solution? Should I use a stack?
 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something like

Then & the mask against the input to see if it's not zero.

0x80000000 is an int with the most significant bit (i.e. left-most bit) set to '1'.
In bits that would be: 10000000 00000000 00000000 00000000

mask >>>= 1 shifts the mask one bit to the right without sign extending the most significant bit.
Yours was starting at the least significant bit and shifting left by one bit.
 
M Hasan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Carey Brown but I really don't have that knowledge to use your for loop cause I don't understand why mask = 0X80000000. I also don't know how to use it.
 
M Hasan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
M Hasan wrote:thanks Carey Brown but now it is giving the same result like my code which is

Please Enter Integer
1
The integer in Bit is :
-2147483648
00000000000000000000000000000001

I thought the answer is 10000000000000000000000000000000
 
M Hasan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
M Hasan wrote:
M Hasan wrote:thanks Carey Brown
 
M Hasan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
M Hasan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe something is not correct cause it is giving the same result as my code
 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't have your problem

I'm guessing you still left this line in (?)

 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
M Hasan wrote:thanks Carey Brown but now it is giving the same result like my code which is

Please Enter Integer
1
The integer in Bit is :
-2147483648
00000000000000000000000000000001

I thought the answer is 10000000000000000000000000000000

Don't know where you got the -2147483648 from but the 000...00001 is the correct bit pattern for the input of "1"

The right most bit is 1, the next one to the left is 2, and the next is 4, etc..
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!