• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Bit Manipulation help needed

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23949
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so what is the solution? Should I use a stack?
 
Saloon Keeper
Posts: 10646
83
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
 
M Hasan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10646
83
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10646
83
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
reply
    Bookmark Topic Watch Topic
  • New Topic