• 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

Integers near zero

 
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have a question about how do integers look like near zero. I mean when they are written bit by bit.

Integers are 32-bit and one of them is the sign bit.

Does it look like that?

1000 0000 0000 0000 0000 0000 0000 0011 //This is int == -4
1000 0000 0000 0000 0000 0000 0000 0010 //This is int == -3
1000 0000 0000 0000 0000 0000 0000 0001 //This is int == -2
1000 0000 0000 0000 0000 0000 0000 0000 //This is int == -1
0000 0000 0000 0000 0000 0000 0000 0000 //This is int == 0
0000 0000 0000 0000 0000 0000 0000 0001 //This is int == 1
0000 0000 0000 0000 0000 0000 0000 0010 //This is int == 2
0000 0000 0000 0000 0000 0000 0000 0011 //This is int == 3


(I grouped them in fours to make them easier to read)
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I think so....
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java stores integers in two's complement format - see that page for a detailed explanation.

You've got the negative numbers wrong.

-1 = 1111 1111 1111 1111 1111 1111 1111 1111
-2 = 1111 1111 1111 1111 1111 1111 1111 1110
etc.
[ May 18, 2008: Message edited by: Jesper Young ]
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Imran Jack:
Yes, I think so....



Not so sure about that. 2's complement method is what most use. Here is a link to help you out

http://heather.cs.ucdavis.edu/~matloff/UnixAndC/CLanguage/Bits.html

However, this has nothing to do with the SCJP
 
reply
    Bookmark Topic Watch Topic
  • New Topic