• 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 numbers

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is yet another question from the Sun Learning center practice exam site.
Given:
1. public class juju {
2. public static void main(String [] args) {
3. double z = 0xFFFFFFFC;
4. int i = 0xFFFFFFDF;
5. double x = z * i;
6. System.out.println(x);
7. }
8. }
What is the result?
A
Compilation fails because of an error on line 3.
B
Compilation fails because of an error on line 5.
C
Compilation succeeds and the program prints "-37.0".
D
Compilation succeeds and the program prints "132.0".
The answer is D because both of those numbers are negative numbers. But how do i know that those two numbers are negative from the reading the code?
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
negative numbers always have the sign bit (the left most bit) set to 1.
z is FFFFFFFC which translates to binary to 11111111 11111111 11111111 11111100
i is FFFFFFDF which translates to binary to 11111111 11111111 11111111 11011111
Now, z and i obviously have their left-most (sign) bit set to 1, so they are both negative numbers.
The rest, namely how you get to 132, is just a matter of translating z and i to decimal and multiplying them:
z = 0xFFFFFFFC = 11111111 11111111 11111111 11111100 = -4
i = 0xFFFFFFDF = 11111111 11111111 11111111 11011111 = -33
z*i = -4 * -33 = 132
[ March 16, 2002: Message edited by: Valentin Crettaz ]
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
learn about hexadecimal number, basically you're looking to see if the most significant bit has been set, if this bit is a (binary) '1', then the number is negative, otherwise it's a positive number
[ March 16, 2002: Message edited by: Rajinder Yadav ]
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anybody have a link to learn about the hexidecimal number format?
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Google > Hexadecimal ?
http://www.webopedia.com/TERM/H/hexadecimal.html
http://www.cs.ualberta.ca/~casey/c101/101notes/hex.html
http://www.jaworski.com/htmlbook/dec-hex.htm
http://www.mindprod.com/jglossh.html#HEX
...
Also, a search in this forum may help
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Valentin Crettaz:
negative numbers always have the sign bit (the left most bit) set to 1.
z is FFFFFFFC which translates to binary to 11111111 11111111 11111111 11111100
i is FFFFFFDF which translates to binary to 11111111 11111111 11111111 11011111
Now, z and i obviously have their left-most (sign) bit set to 1, so they are both negative numbers.
The rest, namely how you get to 132, is just a matter of translating z and i to decimal and multiplying them:
z = 0xFFFFFFFC = 11111111 11111111 11111111 11111100 = -4
i = 0xFFFFFFDF = 11111111 11111111 11111111 11011111 = -33
z*i = -4 * -33 = 132
[ March 16, 2002: Message edited by: Valentin Crettaz ]


Hi, this is my first post to JavaRanch!
I'm a bit confused how 0xFFFFFFFC = -4
I thought you converted FFFFFFFC to dec as follows:
15*(16^7) + 15*(16^6) + 15*(16^5) + 15*(16^4) + 15*(16*3) + 15*(16^2) + 15*(16^1) + 12*(16^0) = 4294967292
Is it something to do with converting it to binary (1111111111111111111111111111100), removing the first bit (the negative sign), and then doing something else?
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Here is the short cut :
~x = -x - 1
In other words

-x = ~x + 1

Even if they give a big number, do not worry about the entire bit operation. For ex double d = 0xFFFFFFFFC

By seeing the letter F, u know the number is negative. Now worry about the last byte (i.e) C
C = 12 (ie) x = 1101
~x = 0010
1 +
------
0011
--------
= - 3
I hope i am clear.
One more trick, By looking at ur example number u know that both the numbers are negative. If u multiply two negative numbers , u get a positive number. Simple the obvious choice is 132.
Regards,
Radha
 
Mark Pinks
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot. I get it now.
But in your example,


C = 12 (ie) x = 1101
~x = 0010
1 +
------
0011
--------
= - 3


...did you really mean:
C = 12 (ie) x = 1100
~x = 0011
1 +
------
0100
--------
= - 4
?
 
raman padma
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
OOOPS. Sorry for the mistake. u r right.
Regards,
Radha Shankar
 
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