Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!
  • 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
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank for the Answer Keith.
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by RAGU KANNAN:
Thank for the Answer Keith.
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the Amazing explanation.

How to convert Negative Decimal value into bytes.

For example 1001 = 1110 1001 (Negative "-")
0001 0110 (Flipa)
0001 0111 (Add One Bit)
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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)
0001 0111 (Add One Bit)
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



I don't understand your question.

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
-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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct, Naseem!
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops, you are right. I had it backwards

~x = -x-1
 
RAGU KANNAN
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the confusion my Question is.

How to fit decimal -1001 into bytes.

Thanks, Raghu.K
 
RAGU KANNAN
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
0001 0111 (Add One Bit)

Result is
So 1001 = -23
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Naseem,

The following example the left-most value is sign indicator.
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)
0001 0111 (Add One Bit)

Result is
So 1001 = -23
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Negative Numbers in Java - Two's Complement
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by RAGU KANNAN:
The following example the left-most value is sign indicator.
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And, as always, this is a 1.4 topic, but it's not on the 5.0 exam!
 
Douglas Chorpita
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 )
stop_worrying_about_bits();

:-)
 
RAGU KANNAN
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic