Results 1 to 19 of 19

# Thread: What does this all mean?

- 06-06-2011, 06:56 AM #1Member
- Join Date
- May 2010
- Posts
- 18

- Rep Power
- 0

## What does this all mean?

Herb Schildt writes: If an integer's value is larger than the range of a byte, it will be reduced modulo (the remainder of an integer division) by the byte's range.

I've been trying to understand that but no luck. I know what modulus is (remainder after division) but I don't understand the rest.

I would post my code but I can't find instructions on this site pertaining to code tags.

- 06-06-2011, 07:03 AM #2
Place [ code ] before and [ /code ] after your code (without the spaces).

In what context is that quote taken from?

- 06-06-2011, 07:09 AM #3Member
- Join Date
- May 2010
- Posts
- 18

- Rep Power
- 0

Java Code:public class TypeCasting { public static void main(String args[]) { byte b; int x; x = 130; b = (byte)x; System.out.println(b); } }

Herb is simply talking about casting incompatible types.

The code prints -126. I'm trying to understand what is happening.

- 06-06-2011, 07:38 AM #4Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,706

- Rep Power
- 13

In binary 130 would be 10000010.

When you cast to byte all but the last 8 bits are thrown away and the result is interpreted in twos complement. There are only 8 bits in 130 (well there are others at the front which are thrown away but they are all zero). The bottom line of twos complement is that if the first bit is a one you have a negative number. You find that number by subtracting the given value from 128.

So, in this case, 130 is 10000010 so it is negative. And the byte represents the value 128-130=-126.

[Edit] Sorry, that's totally bogus. I was thinking one thing and writing another. Once you've identified the number as negative you get its magnitude by looking at the remaining seven bits and subtracting that from 128. ie 130=**1**0000010 so its negative and 0000010=2, 128-2=126 so the byte is -126.Last edited by pbrockway2; 06-06-2011 at 07:48 AM.

- 06-06-2011, 07:42 AM #5
I'll explain it in terms of binary numbers.

The int 130 is 0000 0000 0000 0000 0000 0000 1000 0010 (32 bits).

No wif we try and stuff that number into a byte which only has 8 bits then only the right hand 8 bits are kept.

Therefore the byte will have the value 1000 0010.

The far left bit indicates the sign (1 is negative and 0 is positive), therefore the number in the byte is negative but what negative number is it?

Java uses 2s compliment for its numbers. To convert the number we invert all the bits (change 1 to 0 and 0 to 1) and then add 1.

Byte: 1000 0010

Inverted: 0111 1101

Add one: 0111 1110

The final binary number is 126. Therefore the value in the byte is -126.

As to what Herb is on about is you can calculate what the final value is by doing some modulo magic on the int value. I'm not a math magician so I'm not sure what the exact formula is.

- 06-06-2011, 07:42 AM #6Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,706

- Rep Power
- 13

I suppose a int->byte recipe might be "add 128, reduce modulo 256, then subtract 128".

Java bytes are signed. Were they unsigned an went from 0->255, then Schildt's simpler expression might make sense.

- 06-06-2011, 08:05 AM #7Member
- Join Date
- May 2010
- Posts
- 18

- Rep Power
- 0

- 06-06-2011, 08:08 AM #8
Ignoring all the other bits apart from the 2 far right bits

1 = 01

2 = 10

Therefore 01 + 01 = 10

- 06-06-2011, 08:41 PM #9Member
- Join Date
- May 2010
- Posts
- 18

- Rep Power
- 0

Why are we adding one anyway? Let's just invert the bits and call it a done deal?

Inverted: 0111 1101

Add one: 0111 1110

Am I supposed to add (binary addition) these two numbers and I end up with -126? Why isn't it:

Inverted: 0111 1101

Add one: 0000 0001

I don't understand all of this yet.

- 06-06-2011, 09:02 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 13,963
- Blog Entries
- 7

- Rep Power
- 22

What you describe is the ones-complement number system; have a look:

Java Code:0 0 0000 1 1 0001 2 2 0010 3 3 0011 4 4 0100 5 5 0101 6 6 0110 7 7 0111 -8 -7 1000 -7 -6 1001 -6 -5 1010 -5 -4 1011 -4 -3 1100 -3 -2 1101 -2 -1 1110 -1 -0 1111

In Java, minus a number can be calculated as follows:

Java Code:int number= ...; int minus= ~number+1;

Java Code:int minus= ~number;

JosI have the stamina of a seal; I lie on the beach instead of running on it.

- 06-06-2011, 09:09 PM #11Member
- Join Date
- May 2010
- Posts
- 18

- Rep Power
- 0

I think what you wrote is not the binary representation of one but the result after adding one. So I think it's like this:

Inverted: 0111 1101

One: 0000 0001

Answer: 0111 1110

So then... 64 + 32 + 16 + 8 +4 +2 = 126

So then do I flip the 0 in the front and make that a 1 so it becomes a negative number (-126)?

- 06-06-2011, 09:31 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 13,963
- Blog Entries
- 7

- Rep Power
- 22

- 06-06-2011, 09:36 PM #13Member
- Join Date
- May 2010
- Posts
- 18

- Rep Power
- 0

What do you mean it doesn't work like that? So then what Junky wrote is wrong?

- 06-06-2011, 09:44 PM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 13,963
- Blog Entries
- 7

- Rep Power
- 22

Just as I wrote: it doesn't work like that and what Junky wrote is correct; you simply misread what Junky and/or I wrote; again: negating a number in the twos-complement system flips all bits and adds one to the result. A 1 as the left most bit is considered a negative number, a 0 is considered a positive number (or zero). Flipping just the left most bit doesn't make much sense.

kind regards,

JosI have the stamina of a seal; I lie on the beach instead of running on it.

- 06-06-2011, 09:46 PM #15Member
- Join Date
- May 2010
- Posts
- 18

- Rep Power
- 0

If you take 130 in decimal and change it to binary it looks like this: 1000 0010.

After doing the two's complement thing and inverting the bits it looks like this: 0111 1101

Now for some queer reason we are adding one (don't know why) so I think we have a binary addition problem next: 0111 1101

0000 0001

Add those two numbers together and you get: 0111 1110 which is 126 in decimal. But it's a positive number.

Don't know why the computer prints -126.

- 06-06-2011, 09:56 PM #16Member
- Join Date
- May 2010
- Posts
- 18

- Rep Power
- 0

But the leftmost bit is 0 not 1 so how can it be a negative number? Look at the final result of adding one to the inverted thing: 0111 1110. The left most bit is 0 so that is a positive 126. Why not just quit there? Like Junky said the final binary number is 126.

Last edited by 357mag; 06-06-2011 at 10:00 PM.

- 06-06-2011, 10:14 PM #17
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 13,963
- Blog Entries
- 7

- Rep Power
- 22

The bit sequence 1000 0010 may be 130 to you but it is a negative number if the bit sequence is considered to be a byte; the leftmost bit tells that it is a negative number. flipping the bits and adding one give you minus this number which is 126 so the original numer (which you interpreted as 130) is -126.

kind regards,

JosI have the stamina of a seal; I lie on the beach instead of running on it.

- 06-06-2011, 10:55 PM #18Member
- Join Date
- May 2010
- Posts
- 18

- Rep Power
- 0

So when I try to stuff an integer value 130 in a byte after losing all the leading zeros I have 1000 0010 which is a negative number since the leftmost bit is a 1 meaning it's a negative number. Then I invert all the bits to get 0111 1101. After I do the math and add that up it comes to 125 in decimal. Then I add 1 to that and I get 126. But it is -126 since the original leftmost bit is a 1 meaning it's a negative number.

Or another way of getting the same answer is to take the 1000 0010 and figuring that is -128 + 2 which is -126.

Hope this is correct.

- 06-07-2011, 08:15 AM #19
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 13,963
- Blog Entries
- 7

- Rep Power
- 22

## Bookmarks