• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Doubt in negative numbers in binary format

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 23928
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

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 ]
 
lowercase baba
Posts: 13073
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In code tags for formating
 
Ranch Hand
Posts: 214
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Deepika Namasani
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you
 
fred rosenberger
lowercase baba
Posts: 13073
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and don't forget the classic example of

1000...0000

which would translate to -0.
 
My previous laptop never exploded like that. Read this tiny ad while I sweep up the shards.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic