This week's book giveaway is in the Big Data forum. We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line! See this thread for details.

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

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

posted

0

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

posted

0

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

posted

0

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

posted

0

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.

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

posted

0

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

posted

0

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.

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

posted

0

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?

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