File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How byte's are terminating the overflow value. 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 "How byte Watch "How byte New topic
Author

How byte's are terminating the overflow value.

RAGU KANNAN
Ranch Hand

Joined: Dec 16, 2005
Posts: 103
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
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Dec 16, 2005
Posts: 103
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Dec 16, 2005
Posts: 103
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

Joined: Feb 07, 2005
Posts: 2367
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 ]
Douglas Chorpita
Ranch Hand

Joined: May 09, 2006
Posts: 97
-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


SCJP 1.4 - 95%
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
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 ]

Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Douglas Chorpita
Ranch Hand

Joined: May 09, 2006
Posts: 97
Correct, Naseem!
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Oops, you are right. I had it backwards

~x = -x-1
RAGU KANNAN
Ranch Hand

Joined: Dec 16, 2005
Posts: 103
Sorry for the confusion my Question is.

How to fit decimal -1001 into bytes.

Thanks, Raghu.K
RAGU KANNAN
Ranch Hand

Joined: Dec 16, 2005
Posts: 103
For the above question the "-" is negative, so i need to convert "-1001" into byte value.

Thanks, Raghu.K
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
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

Joined: Dec 16, 2005
Posts: 103
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

Joined: Apr 25, 2005
Posts: 809
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

Joined: Dec 16, 2005
Posts: 103
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
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Negative Numbers in Java - Two's Complement
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
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 ]
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
And, as always, this is a 1.4 topic, but it's not on the 5.0 exam!


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Douglas Chorpita
Ranch Hand

Joined: May 09, 2006
Posts: 97
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

Joined: May 09, 2006
Posts: 97
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

Joined: Dec 16, 2005
Posts: 103
Hello Doug,

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

Thanks, Raghu.K
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How byte's are terminating the overflow value.