sumit bajaj

Greenhorn

Posts: 10

posted 10 years ago

Yes, but what's your question?

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." *~Joe Strummer*

sscce.org

sumit bajaj

Greenhorn

Posts: 10

posted 10 years ago

Actually i want this explanatio.

For eg:-- if i have Decimal Number byte b=127. It's Binary is 01111111. i want to shift it left like . b<<1 then surely binary will be 11111110 .ie. it's nEgative number bacuase left most bit is 1. but how the answer came (-2). i didn't get this thing. please help me.

For eg:-- if i have Decimal Number byte b=127. It's Binary is 01111111. i want to shift it left like . b<<1 then surely binary will be 11111110 .ie. it's nEgative number bacuase left most bit is 1. but how the answer came (-2). i didn't get this thing. please help me.

Remko Strating

Ranch Hand

Posts: 893

posted 10 years ago

In Java the integer is a signed two�s complement integer

The following link will explain that for you

[url=http://en.wikipedia.org/wiki/Two%27s_complement]Two's complement[/url]

So 11111110 = -2.

The following link will explain that for you

[url=http://en.wikipedia.org/wiki/Two%27s_complement]Two's complement[/url]

So 11111110 = -2.

Remko (My website)

SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,

shankar reddy

Ranch Hand

Posts: 71

posted 10 years ago

given 127<<1

its binary form 01111111

we need to shift 1 bit field so

it will become 11111110.

because of the MSB is 1 .. the result will be negative number.

in order to get the value from 11111110

use 2's complement.

1's complement. 00000001

add 1 1

---------

00000010

it gives 2.

beacuse of MSB (negative) the result is -2

its binary form 01111111

we need to shift 1 bit field so

it will become 11111110.

because of the MSB is 1 .. the result will be negative number.

in order to get the value from 11111110

use 2's complement.

1's complement. 00000001

add 1 1

---------

00000010

it gives 2.

beacuse of MSB (negative) the result is -2

Java Lover<br /> <br />Shankar Reddy <br />SCJP1.4 (88%)

posted 10 years ago

This has nothing to do with shifting of negative numbers -- it's about how signed values are stored in binary format. As described above, you basically get the value by inverting the bits, then adding 1...

11111110 original

00000001 inverting bits

00000010 adding one

This value is 2, so the original value is -2.

Originally posted by sumit bajaj:

...binary will be 11111110 .ie. it's nEgative number bacuase left most bit is 1. but how the answer came (-2)...

This has nothing to do with shifting of negative numbers -- it's about how signed values are stored in binary format. As described above, you basically get the value by inverting the bits, then adding 1...

11111110 original

00000001 inverting bits

00000010 adding one

This value is 2, so the original value is -2.

*~Joe Strummer*

sscce.org