# How byte's are terminating the overflow value.

RAGU KANNAN

Ranch Hand

Posts: 103

Keith Lynn

Ranch Hand

Posts: 2409

posted 10 years ago

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.

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

posted 10 years ago

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

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: 2409

posted 10 years ago

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.

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

posted 10 years ago

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

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: 2409

posted 10 years ago

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 ]

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

Posts: 97

posted 10 years ago

-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

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

Posts: 809

posted 10 years ago

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 ]

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 ]

Keith Lynn

Ranch Hand

Posts: 2409

RAGU KANNAN

Ranch Hand

Posts: 103

RAGU KANNAN

Ranch Hand

Posts: 103

Naseem Khan

Ranch Hand

Posts: 809

RAGU KANNAN

Ranch Hand

Posts: 103

posted 10 years ago

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

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

RAGU KANNAN

Ranch Hand

Posts: 103

posted 10 years ago

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

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

Posts: 2023

Naseem Khan

Ranch Hand

Posts: 809

posted 10 years ago

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 ]

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

Sheriff

Posts: 8900

5

Douglas Chorpita

Ranch Hand

Posts: 97

Douglas Chorpita

Ranch Hand

Posts: 97

posted 10 years ago

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();

:-)

-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();

:-)

SCJP 1.4 - 95%