Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!
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
• Ron McLeod
• Tim Cooke
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• Junilu Lacar
• Rob Spoor
• Jeanne Boyarsky
Saloon Keepers:
• Stephan van Hulst
• Carey Brown
• Tim Holloway
• Piet Souris
Bartenders:

# How byte's are terminating the overflow value.

Ranch Hand
Posts: 103
• • Number of slices to send:
Optional 'thank-you' note:
• • Hello,

127 in binary is 01111111
Shifted left one position, it becomes 11111110. equals to 254.
The value 254, doesn't fall within the range of a byte � this is overflow.
So the byte result is -2. I would like to know what is logic for byte termination

Thanks, Raghu.K

Ranch Hand
Posts: 2412
• • Number of slices to send:
Optional 'thank-you' note:
• • Originally posted by RAGU KANNAN:
Hello,

127 in binary is 01111111
Shifted left one position, it becomes 11111110. equals to 254.
The value 254, doesn't fall within the range of a byte � this is overflow.
So the byte result is -2. I would like to know what is logic for byte termination

Thanks, Raghu.K

No the value is not 254.

The first bit is the sign bit. It determines whether the number is positive or negative.

Since the first bit is 1, 11111110 is a negative number.

To determine what negative number it is, simply flip all the bits, and add 1.

00000001
+ 00000001

00000010

So the negative number is -2.

RAGU KANNAN
Ranch Hand
Posts: 103
• • Number of slices to send:
Optional 'thank-you' note:
• • I have one more example wants to clarify.

For example I try to assign 1794 In bytes.
The byte values for 1794 is 0110 0000 0010. so it's positive value
and i am fliping the bit 0001 1111 1101 (I am not toching the sign bit)
and adding 1 bit 0001 1111 1110
From this how the system will find the "10" equals to "2".

Thanks, Raghu.k

Keith Lynn
Ranch Hand
Posts: 2412
• • Number of slices to send:
Optional 'thank-you' note:
• • Originally posted by RAGU KANNAN:
I have one more example wants to clarify.

For example I try to assign 1794 In bytes.
The byte values for 1794 is 0110 0000 0010. so it's positive value
and i am fliping the bit 0001 1111 1101 (I am not toching the sign bit)
and adding 1 bit 0001 1111 1110
From this how the system will find the "10" equals to "2".

Thanks, Raghu.k

In this case, you would not flip the bits and add 1. That is only used when you have a negative number.

-x = ~x-1

In this case, when you cast 1794 to a byte, only the lower 8 bits are used.

So you have 00000010 which is 2.

The other bits are lost.

RAGU KANNAN
Ranch Hand
Posts: 103
• • Number of slices to send:
Optional 'thank-you' note:
• • Thanks for the Amazing explanation.

How to convert Negative Decimal value into bytes.

For example 1001 = 1110 1001 (Negative "-")
0001 0110 (Flipa)
So 1001 = -23

I would like to produce -1001 = 23, I don't know how to convert this,
Pls explain to me.

Thanks, Raghu.K

Keith Lynn
Ranch Hand
Posts: 2412
• • Number of slices to send:
Optional 'thank-you' note:
• • Originally posted by RAGU KANNAN:
Thanks for the Amazing explanation.

How to convert Negative Decimal value into bytes.

For example 1001 = 1110 1001 (Negative "-")
0001 0110 (Flipa)
So 1001 = -23

I would like to produce -1001 = 23, I don't know how to convert this,
Pls explain to me.

Thanks, Raghu.K

If you look at the bit pattern

00001001 = 9,

the negative of this is

11110110
+ 00000001

11110111 = -9
[ July 27, 2006: Message edited by: Keith Lynn ]

Ranch Hand
Posts: 97
• • Number of slices to send:
Optional 'thank-you' note:
• • -1001 fits in a short, so we calculate with a short.

Decimal 1001 -> Binary 0000 0011 1110 1001 -> Hex 03E9
Flip -> Binary 1111 1100 0001 0110 -> Hex FC16
Add One -> Binary 1111 1100 0001 0111 -> Hex FC17
Decimal -1001 -> Binary 1111 1100 0001 0111 -> Hex FC17
Shorten to byte -> Binary 0001 0111 -> Hex 17

Hex 17 = 1 x 16 + 7 = 23

Ranch Hand
Posts: 809
• • Number of slices to send:
Optional 'thank-you' note:
• • Hi Keith,

I think it should be -x = ~x+1 istead of -x = ~x-1 as you posted.

Naseem
[ July 27, 2006: Message edited by: Naseem Khan ]

Douglas Chorpita
Ranch Hand
Posts: 97
• • Number of slices to send:
Optional 'thank-you' note:
• • Correct, Naseem!

Keith Lynn
Ranch Hand
Posts: 2412
• • Number of slices to send:
Optional 'thank-you' note:
• • Oops, you are right. I had it backwards

~x = -x-1

RAGU KANNAN
Ranch Hand
Posts: 103
• • Number of slices to send:
Optional 'thank-you' note:
• • Sorry for the confusion my Question is.

How to fit decimal -1001 into bytes.

Thanks, Raghu.K

RAGU KANNAN
Ranch Hand
Posts: 103
• • Number of slices to send:
Optional 'thank-you' note:
• • For the above question the "-" is negative, so i need to convert "-1001" into byte value.

Thanks, Raghu.K

Naseem Khan
Ranch Hand
Posts: 809
• • Number of slices to send:
Optional 'thank-you' note:
• • Originally posted by RAGU KANNAN Sorry for the confusion my Question is.

How to fit decimal -1001 into bytes.

Thanks, Raghu.K

Are you asking how to get binary representation of -1001? or how to get byte value like this:

byte b=(byte)-1001; // b gives you 23.

Naseem

RAGU KANNAN
Ranch Hand
Posts: 103
• • Number of slices to send:
Optional 'thank-you' note:
• • Hello Guy�s

Sorry again for mix-up.

Here is the step-by-step process how the system will convert decimal to bytes And this example is for Positive decimal value �1001�. I have little bit confuse about converting Negative decimal value to bytes. So I need to know the same step-by-step process for negative decimal value �-1001�. Pls help me how to do this.

Thanks, Raghu.K

For example �1001 �

1110 1001 (Negative "-")
0001 0110 (Flip)

Result is
So 1001 = -23

Naseem Khan
Ranch Hand
Posts: 809
• • Number of slices to send:
Optional 'thank-you' note:
• • If you want to get binary representation of -1001.

You can do this by....

-x = ~x + 1

-1001 = ~(1001) + 1

Means find the one's complement of 1001 and then add 1. You will get binary equivalent of -1001

Naseem

RAGU KANNAN
Ranch Hand
Posts: 103
• • Number of slices to send:
Optional 'thank-you' note:
• • Naseem,

That is reason the byte value for " 1001 " is "-23" (Negative -23).
So the -1001 also have Left most sign bit. but the result is positive "23"
I would like to know how?

For example �1001 �

1110 1001 (Negative "-")
0001 0110 (Flip)

Result is
So 1001 = -23

Ranch Hand
Posts: 2023
• • Number of slices to send:
Optional 'thank-you' note:
• • Negative Numbers in Java - Two's Complement

Naseem Khan
Ranch Hand
Posts: 809
• • Number of slices to send:
Optional 'thank-you' note:
• • Originally posted by RAGU KANNAN:
That is reason the byte value for " 1001 " is "-23" (Negative -23).

NO, You are basically mixing binary representation and java's byte primitive type.

The byte value of 1001 is -23. Negative is not because of left-most bit is 1. 1001 is not at all in binary rather its a decimal number.

byte b=(byte)1001; here b's value is -23.

Range of 1001 is outside -128 to 127. So there will be a narrowing.

Forget 1001. I am taking say 2001 and its byte value is coming -47.

For -1001...

byte b=(byte)-1001; again -1001 is in DECIMAL NOT BINARY

Naseem
[ July 27, 2006: Message edited by: Naseem Khan ]

author Posts: 9050
21
• • Number of slices to send:
Optional 'thank-you' note:
• • And, as always, this is a 1.4 topic, but it's not on the 5.0 exam!

Douglas Chorpita
Ranch Hand
Posts: 97
• • Number of slices to send:
Optional 'thank-you' note:
• • One more time:

Decimal -1001 -> Binary 1111 1100 0001 0111 -> Hex FC17 (see above)
Narrowed to byte -> Binary 0001 0111 -> Hex 17

Hex 17 = 1 x 16 + 7 = Decimal 23

Douglas Chorpita
Ranch Hand
Posts: 97
• • Number of slices to send:
Optional 'thank-you' note:
• • Perhaps an easier example:

-255

Start: 255 Decimal -> 0000 0000 1111 1111 Binary -> 00FF Hex
Flip: -256 Decimal -> 1111 1111 0000 0000 Binary -> FF00 Hex
+1: -255 Decimal -> 1111 1111 0000 0001 Binary -> FF01 Hex
narrow to byte -> 0000 0001 Binary -> 01 Hex -> 1 Decimal

if( (byte) -255 == (byte) 1 )

:-)

RAGU KANNAN
Ranch Hand
Posts: 103
• • Number of slices to send:
Optional 'thank-you' note:
• • Hello Doug,

Can you give me the similar example for Positive "255".

Thanks, Raghu.K I'm so happy! And I wish to make this tiny ad happy too: The Low Tech Laboratory Movie Kickstarter is LIVE NOW! https://www.kickstarter.com/projects/paulwheaton/low-tech