• 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

Different between signs(Positive and Negative ) and values

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question: -
The following example is part of Kathy certification book.
The following examples shows hexadecimal value equals to binary (8 = 1000)
But the definition says left most 1 is equal to negative sign.
How can I differenciate which one is value (8=1000) and which one is negative or positive sign value?



Example :-

class BitShift {
public static void main(String [] args) {
int x = 0x80000000;
System.out.println("Before shift x equals " + x);
x = x << 1;
System.out.println("After shift x equals " + x);
}
}

To understand the preceding example, we�ll convert the hexadecimal number to a
bit number. Fortunately, it�s pretty simple to convert from hexadecimal to bits. Each
hex digit converts to a four-bit representation, as we can see here:

8 0 0 0 0 0 0 0
1000 0000 0000 0000 0000 0000 0000 0000

In the preceding example, the very leftmost bit represents the sign (positive or
negative). When the leftmost bit is 1, the number is negative; and when it is 0, the
number is positive. Running our program gives us the following:

%java BitShift
Before shift x equals -2147483648
After shift x equals 0
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
every primitive has a size , for ex. int holds 32 bits. here the most significant bit (the leftmost) is the sign bit. the rest are the values.
so if you have int x = 8, then the binary eq. is
00000000000000000000000000001000 ( x = 8 )
11111111111111111111111111111000 ( x = -8 ) just a 2's complement on +8.


hth.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See this thread for more details.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic