sanjana narayanan

Ranch Hand

Posts: 142

Marlene Miller

Ranch Hand

Posts: 1392

Vicken Karaoghlanian

Ranch Hand

Posts: 522

posted 13 years ago

Hi sanjana,

The output is -126 and it is NOT 126. MM explained it clearly, however i want to add some notes. If you encounter such a question in the exam, the easiest way to solve it is by this:

|--------|-------|

-128 <-- 0 --> 127

start counting from zero, when you reach to the end (i.e 127) then continue count from -128.

in your example i = 130

start counting until you reach 127 then continue -128, -127 and finally -126 (Which is your answer).

hope this helps.

[ December 03, 2003: Message edited by: Vicken Karaoghlanian ]

The output is -126 and it is NOT 126. MM explained it clearly, however i want to add some notes. If you encounter such a question in the exam, the easiest way to solve it is by this:

|--------|-------|

-128 <-- 0 --> 127

start counting from zero, when you reach to the end (i.e 127) then continue count from -128.

in your example i = 130

start counting until you reach 127 then continue -128, -127 and finally -126 (Which is your answer).

hope this helps.

[ December 03, 2003: Message edited by: Vicken Karaoghlanian ]

- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!

CH Lee

Ranch Hand

Posts: 42

sanjana narayanan

Ranch Hand

Posts: 142

Marlene Miller

Ranch Hand

Posts: 1392

posted 13 years ago

I'd like to suggest another way to think about 130.

A byte has 8 bits. 8 bits can hold 256 unsigned numbers, 0 to 255. The first half of those 256 numbers are 0 to 127. The second half 128 to 255 are designated as the negative numbers of the first half.

This is the most important fact of two's complement arithmetic:

x + (-x) == 2^8 == 256

130 belongs to the second half. It represents the negative of some number in the first half.

126 + 130 == 256

130 represents -126.

[ December 03, 2003: Message edited by: Marlene Miller ]

A byte has 8 bits. 8 bits can hold 256 unsigned numbers, 0 to 255. The first half of those 256 numbers are 0 to 127. The second half 128 to 255 are designated as the negative numbers of the first half.

This is the most important fact of two's complement arithmetic:

x + (-x) == 2^8 == 256

130 belongs to the second half. It represents the negative of some number in the first half.

126 + 130 == 256

130 represents -126.

[ December 03, 2003: Message edited by: Marlene Miller ]

CH Lee

Ranch Hand

Posts: 42

posted 13 years ago

Hey guys,

I have done some thinking on the solution shown by Vicken. I must say this again that it's great and I am gonna use his solution, with some 'customisation' to make me understand better and hopefully the rest as well.

I find that calculating in Vicken's could lead me to more mistake (I am a very careless person ), so I 'device' this method.

Vicken, please verify this for me. Thanks.

Let's take the same example.

int i = 130;

byte b = (byte) i;

Decimal 130 = Binary 10000010

In order to get the -ve value, except the left most '1', I just 'flip' all binary value (do a XOR?), then I will get:

Binary 11111101 = Decimal 253.

Because the max of =ve value is 127, so I deduct 127 from the above value.

253 - 127 = 126

And the value is -126 (must remember to put back the - sign as we are in the -ve zone).

Does this sound good?

As for value 256, (Binary 11111111), after flipping all the value, it becomes 10000000, so it's basically 0.

Thanks Vicken for this idea. If I get all questions on this kind in SCJP 1.4, then I won't have to be ashame of posting it here.

Cheers.

I have done some thinking on the solution shown by Vicken. I must say this again that it's great and I am gonna use his solution, with some 'customisation' to make me understand better and hopefully the rest as well.

I find that calculating in Vicken's could lead me to more mistake (I am a very careless person ), so I 'device' this method.

Vicken, please verify this for me. Thanks.

Let's take the same example.

int i = 130;

byte b = (byte) i;

Decimal 130 = Binary 10000010

In order to get the -ve value, except the left most '1', I just 'flip' all binary value (do a XOR?), then I will get:

Binary 11111101 = Decimal 253.

Because the max of =ve value is 127, so I deduct 127 from the above value.

253 - 127 = 126

And the value is -126 (must remember to put back the - sign as we are in the -ve zone).

Does this sound good?

As for value 256, (Binary 11111111), after flipping all the value, it becomes 10000000, so it's basically 0.

Thanks Vicken for this idea. If I get all questions on this kind in SCJP 1.4, then I won't have to be ashame of posting it here.

Cheers.

Ray Stojonic

Ranch Hand

Posts: 326

posted 13 years ago

As Marlene previously showed, the easiest (and safest) way to do this is to learn and use 2s complement.

1000 0010 == 130 only in a buffer > 8 bits, or an 8 buffer that is unsigned

(like char, for instance)

A byte is signed, so we know that the number, as a byte, is negative because the leftmost bit is on.

2s complement shows us a value of 126, so the inverse value (the value we started with) must be -126

256 == 1 0000 0000 by my count is one more bit than 8, so it doesn't fit in an 8 bit buffer. byte value == 0

1111 1111 == -1 in an signed 8 bit buffer (or 255 unsigned)

(works for signed whole number buffers of

or

you be the judge

[ December 04, 2003: Message edited by: Ray Stojonic ]

1000 0010 == 130 only in a buffer > 8 bits, or an 8 buffer that is unsigned

(like char, for instance)

A byte is signed, so we know that the number, as a byte, is negative because the leftmost bit is on.

2s complement shows us a value of 126, so the inverse value (the value we started with) must be -126

256 == 1 0000 0000 by my count is one more bit than 8, so it doesn't fit in an 8 bit buffer. byte value == 0

1111 1111 == -1 in an signed 8 bit buffer (or 255 unsigned)

(works for signed whole number buffers of

**any**size)or

you be the judge

[ December 04, 2003: Message edited by: Ray Stojonic ]

Marlene Miller

Ranch Hand

Posts: 1392

posted 13 years ago

There are two ways to find the 8-bit representation of �b, if 0 <= b <= 127

or to find b, if 128 <= 8-bit representation of -b <= 255.

256 � b

~b + 1

----

For example, to find the 8-bit representation of -126?

256 � 126 == 130 == 10000010

~(01111110) + 1 == 10000001 + 1 == 10000010 == 130

----

For example, 130 represents the negative of what number?

256 � 130 == 126

~(10000010) + 1 == 01111101 + 1 == 01111110 == 126

[ December 04, 2003: Message edited by: Marlene Miller ]

or to find b, if 128 <= 8-bit representation of -b <= 255.

256 � b

~b + 1

----

For example, to find the 8-bit representation of -126?

256 � 126 == 130 == 10000010

~(01111110) + 1 == 10000001 + 1 == 10000010 == 130

----

For example, 130 represents the negative of what number?

256 � 130 == 126

~(10000010) + 1 == 01111101 + 1 == 01111110 == 126

[ December 04, 2003: Message edited by: Marlene Miller ]

Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |