• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Negative Hexadecimal number

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can you determine a Hexadecimail number is negative?
Look at the following:

int z=0xdeadcafe;
System.out.println("z=" + z);

z=-559035650

??
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Each hexadecimal "digit" is 4 bits. The d in the high order position is 1101. So you see it's got a high bit of one, therefore the whole number is negative.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Barry's absolutely correct.

How do you know if a binary number is negative? You look at the most significant bit (the sign bit) to see if it is a 1 or a 0. If it's a 1, you have a negative number. If it's a 0, it's positive.

So how does this translate to hex? Well, like Barry said, every hexadecimal digit is equivalent to 4 binary digits. So, take the most significant hexadecimal digit and determine if the binary bits it represents start with a 1 or not. In hex, anything 8 or greater starts with a 1 so, if your hexadecimal number starts with a digit greater than or equal to 8, it's negative. If it's 0 through 7, it's positive.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int z=0xdeadcafe;
System.out.println("z=" + z);

z=-0xdeadcafe
System.out.println("z=" + z);

This makes you to assign a negative hexadecimal number
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Corey McGlone wrote:Barry's absolutely correct.

How do you know if a binary number is negative? You look at the most significant bit (the sign bit) to see if it is a 1 or a 0. If it's a 1, you have a negative number. If it's a 0, it's positive.

So how does this translate to hex? Well, like Barry said, every hexadecimal digit is equivalent to 4 binary digits. So, take the most significant hexadecimal digit and determine if the binary bits it represents start with a 1 or not. In hex, anything 8 or greater starts with a 1 so, if your hexadecimal number starts with a digit greater than or equal to 8, it's negative. If it's 0 through 7, it's positive.




Hi,

But if the same code is tested with hex 0xdcafe which is also starting with d, the output is still positive. Here,d nibble binary representation starts with 1, why isn't it is negative?
Can you please explain?

Thanks,
bharadwaj
 
Saloon Keeper
Posts: 7355
170
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A hex number is always positive (unless you specifically put a minus sign in front of it). It might be interpreted as a negative number once you store it in a particular data type. Only then does the most significant bit (MSB) matter, but it's the MSB of the number "as stored in that data type". In that respect the answers above are only partially correct: only in the context of an actual data type (like an int or a long) does the MSB matter.

If you store "0xdcafe" in an int, the representation of it would be "0000 0000 0000 1101 1100 1010 1111 1110" - the MSB is 0. Whereas the representation of "0xdeadcafe" is "1101 1110 1010 1101 1100 1010 1111 1110" - the MSB is 1.
 
Bharadwaj Vanamamalai
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Moores wrote:A hex number is always positive (unless you specifically put a minus sign in front of it). It might be interpreted as a negative number once you store it in a particular data type. Only then does the most significant bit (MSB) matter, but it's the MSB of the number "as stored in that data type". In that respect the answers above are only partially correct: only in the context of an actual data type (like an int or a long) does the MSB matter.

If you store "0xdcafe" in an int, the representation of it would be "0000 0000 0000 1101 1100 1010 1111 1110" - the MSB is 0. Whereas the representation of "0xdeadcafe" is "1101 1110 1010 1101 1100 1010 1111 1110" - the MSB is 1.



Thanks Moores for your clarification.
 
author
Posts: 23923
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

Also, when you assign a positive number to a variable, and get a negative number, technically, the number was too big for the variable (and overflowed). The designers of Java decided to allow this because using hex numbers to assign bit patterns was common practice. This is why the compiler doesn't complain when you do it.

Henry
 
Ranch Hand
Posts: 30
Eclipse IDE C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like Tim said "A hex number is always positive (unless you specifically put a minus sign in front of it)" is almost correct. Let's make it clear with some examples.

1. Generally, prefix a minus sign to make a HEX number negative.

2. But, during binary conversion the MSB(Most Significant Bit) of the declared container decides positive or negative.

e.g :
Range of int in Java is -2,147,483,648 to -1 OR 0 to 2,147,483,647 in decimal.

0x7FFF FFFF = 2,147,483,647 and when going downwards everything is positive up to 0x0000 0000 (simply 0)
BUT
0x8000 0000 will result -2,147,483,648 and the minus sign will be automatically given (Note : you cannot change the sign manually by prefixing a minus)
THEN
0x8000 0001 will give you -2,147,483,647 and here you can manually change the sign.
AND THEN
if we increase HEX value to 0xFFFF FFFE it will give you -2
THEN AGAIN
+1 HEX 0xFFFF FFFF will result -1
FINALLY
if you put another extra digit like 0xFFFF FFFF 1 the Javac will become red and spit something like "integer number too large: ffffffff1"

3. Inside the int container the range 0x0000 0000 - 0xFFFF FFFF is just fine.

Summary
-----------
1. 0x0000 0000 = 0
2. 0x0000 0002 = 2
3. 0x7FFF FFFF = 2,147,483,647 (Upper int limit +2^31 -1)
4. 0x8000 0000 = -2,147,483,648 (Lower int limit -2^31)
5. 0x8000 0001 = -2,147,483,647
6. 0xFFFF FFFE = -2
7. 0xFFFF FFFF = -1

4. If the container is big, then it can accommodate more.
e.g : Long x = 0xFFFF FFFFL; ==> sop(x) will print 4,294,967,295 NOT -1

Amazing Java!!!
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic