• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

toHexString() for negative arguments

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
can anyone plz help in 'easily' working out what toHexString(args) produces when args is negative??, for int, long etc. :roll:
thanks in advance.
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nadeem
Well I don't think that a -ve number would behave differently from a +ve one when passed into the toHexString() method. Do you know any contradictions? I tried out a few codes and I don't seem to find any.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1. Convert the positive number to binary
decimal 12 == binary 1100
0000 0000 0000 0000 0000 0000 0000 1100
2a. Flip the bits
0000 0000 0000 0000 0000 0000 0000 1100
1111 1111 1111 1111 1111 1111 1111 0011
2b. and add 1
1111 1111 1111 1111 1111 1111 1111 0011
0000 0000 0000 0000 0000 0000 0000 0001
1111 1111 1111 1111 1111 1111 1111 0100 result
3. Convert to hexadecimal
decimal �12 == hexadecimal fffffff4
 
G Nadeem
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks, Marline and Anupam, for prompt response.
Anupam, this is why i think they differ,

The unsigned integer value is the argument plus 2^32 if the argument is negative; otherwise, it is equal to the argument.

(Integer documentation)
Marline ur 'formula' was helpful.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The unsigned integer value is the argument plus 2^32 if the argument is negative; otherwise, it is equal to the argument.


Suppose for the moment type int was only 8 bits instead of 32 bits.
-1 + 2^8 = -1 + 256 = 255
255 is binary 1111 1111
Now back to the real world where type int is 32 bits.
-1 + 2^32 = -1 + 4294967296 = 4294967295
4294967295 is binary 1111 1111 1111 1111 1111 1111 1111 1111
In general, to find the binary representation of a negative number,
take the negative number, add 2^32, then convert to binary.
Sometimes it is easier to take the positive number, convert to binary, flip the bits and add 1.
[ May 20, 2003: Message edited by: Marlene Miller ]
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marlene

What abt this operator ?
~ 4
let's say 0000 0000 0000......0100
then flip 1111 1111 1111 .....1011 // is -5
How do you know it is -5 ?
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1111 1111 1111 1111 1111 1111 1111 1011 What is this?
Flip the bits and add 1. That will give you the positive number.
0000 0000 0000 0000 0000 0000 0000 0100 flip the bits
0000 0000 0000 0000 0000 0000 0000 0101 add 1
For any (positive or negative) binary number, except 0 and the largest negative number, when you flip the bits and add 1, you get the other (negative or positive) number.
For 0, when you flip the bits and add 1, you get the same number (with an overflow).
For the largest negative number (1000 ... 0000), when you flip the bits and add 1, you get the same number.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Before anyone objects to what I just said (because binary numbers are not negative), I am going to correct it. But I am not going to edit the original, because I like the way I said it. But for the purest...
Suppose the integral type has n bits.
For any number binary representation b1 having n bits, if you flip the bits and add 1, you get another binary representation b2 such that b1 + b2 = 2^n.
In two�s complement arithmetic, one of b1 and b2 represents a positive number. The other represents the negative. The exceptions are 0 and 1000...0000.
Sorry Robbie, I had to ward off the critics.
[ May 21, 2003: Message edited by: Marlene Miller ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic