wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in  negative numbers in binary format Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in  negative numbers in binary format" Watch "Doubt in  negative numbers in binary format" New topic
Author

Doubt in negative numbers in binary format

Deepika Namasani
Greenhorn

Joined: May 19, 2005
Posts: 25
public class test33
{
public static void main(String args[])
{
int i = -1;
i = i >> 1;
System.out.println(i);
}
}
answer for this code is -1


since it is -1 something like this

since i is int it is 32 bit(4 byte)

10000000 00000000 00000000 00000001

but when it is >>1(signed right shift it is )

11000000 00000000 00000000 00000000

so how did -1 come

pls clarify this one
thanks


Deepika N
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

since it is -1 something like this

since i is int it is 32 bit(4 byte)

10000000 00000000 00000000 00000001


-1 = 11111111 11111111 11111111 11111111

Java like many other languages/processors uses two's complement. To negate a number you need to complement the number and add one.

Henry
[ May 24, 2005: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11356
    
  16

since it is -1 something like this

since i is int it is 32 bit(4 byte)

10000000 00000000 00000000 00000001


this last line is incorrect. to get the binary representation of a number, follow these steps:

write the positive number in binary. so for -1, we'd write 1 in binary:

00000000 00000000 00000000 00000001

now, flip every bit to it's alternate state:

11111111 11111111 11111111 11111110

Last, add 1:

11111111 11111111 11111111 11111111



now that we have -1 in binary, we can do the bit shift. move everything over one bit, keep the sign bit, and we get:

11111111 11111111 11111111 11111111

which is -1.
[ May 25, 2005: Message edited by: fred rosenberger ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
ok lowercase DAd...

but can u pls tell me that why we do
two complement for getting nagative...

why don't

-1 is like 10000000 00000000 00000000 00000001

as we have negative signbit (1 on leftmost) and rest of the no.

then why we complement the bits and add one

i think we add one becasue negative value has one no. more capacity w.r.t positive no.

but why complements ?
plss explain.


Thanks and Regards, Amit Taneja
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
In code tags for formating
Edwin Keeton
Ranch Hand

Joined: Jul 10, 2002
Posts: 214

The main reason for using twos complement to represent negative numbers is to make it easy to add (and subtract) two signed numbers. Adding 17 and -4 looks like this in binary (showing only 8 bits to keep it short):

0001 0001 (17)
+ 1111 1100 (-4)

= 0000 1101 (13)

It is more expensive to basic arithmetic without using twos complement.

The reason there is one more negative value to a signed integer type is because we use twos complement, not the other way around.


SCJP, SCWCD
Deepika Namasani
Greenhorn

Joined: May 19, 2005
Posts: 25
Thank you
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11356
    
  16

and don't forget the classic example of

1000...0000

which would translate to -0.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in negative numbers in binary format