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
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
so what is the solution? Should I use a stack?

Saloon Keeper
Posts: 10646
83
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:

M Hasan wrote:thanks Carey Brown but now it is giving the same result like my code which is

1
The integer in Bit is :
-2147483648
00000000000000000000000000000001

I thought the answer is 10000000000000000000000000000000

M Hasan
Ranch Hand
Posts: 35
• Number of slices to send:
Optional 'thank-you' note:

M Hasan wrote:

M Hasan wrote:thanks Carey Brown

M Hasan
Ranch Hand
Posts: 35
• Number of slices to send:
Optional 'thank-you' note:
Thanks

M Hasan
Ranch Hand
Posts: 35
• Number of slices to send:
Optional 'thank-you' note:
I believe something is not correct cause it is giving the same result as my code

Carey Brown
Saloon Keeper
Posts: 10646
83
• Number of slices to send:
Optional 'thank-you' note:

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

Carey Brown
Saloon Keeper
Posts: 10646
83
• Number of slices to send:
Optional 'thank-you' note:

M Hasan wrote:thanks Carey Brown but now it is giving the same result like my code which is

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..