Results 1 to 19 of 19
  1. #1
    357mag is offline Member
    Join Date
    May 2010
    Posts
    18
    Rep Power
    0

    Default 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.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Place [ code ] before and [ /code ] after your code (without the spaces).

    In what context is that quote taken from?

  3. #3
    357mag is offline Member
    Join Date
    May 2010
    Posts
    18
    Rep Power
    0

    Default

    Java Code:
    public class TypeCasting
    {
    	public static void main(String args[])
    	{
    		byte b;
    		int x;
    		
    		x = 130;
    		
    		b = (byte)x;
    		
    		System.out.println(b);
    	}
    }
    I made the above program to see what happens if you try to place an integer value that is a little too large to fit into a byte.

    Herb is simply talking about casting incompatible types.

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

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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=10000010 so its negative and 0000010=2, 128-2=126 so the byte is -126.
    Last edited by pbrockway2; 06-06-2011 at 08:48 AM.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

  7. #7
    357mag is offline Member
    Join Date
    May 2010
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    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.
    How can adding one be 0111 1110? I thought the far right digit represents 2 to the 0 power which is 1. And then the column to the left of that would be 2 to the 1 power or 2?

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Ignoring all the other bits apart from the 2 far right bits
    1 = 01
    2 = 10

    Therefore 01 + 01 = 10

  9. #9
    357mag is offline Member
    Join Date
    May 2010
    Posts
    18
    Rep Power
    0

    Default

    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.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by 357mag View Post
    Why are we adding one anyway? Let's just invert the bits and call it a done deal?
    I don't understand all of this yet.
    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
    The first column is the decimal representation of the twos-complement, the second column is the decimal representation of the ones-complement and the last column is just the binary sequence of four bits; the leftmost bit is considered the sign bit. Note that the ones-complement has two different representations for the number zero, which can be quite a burden.
    In Java, minus a number can be calculated as follows:

    Java Code:
    int number= ...;
    int minus= ~number+1;
    Would Java have used ones-complement, the last statement would've been:

    Java Code:
    int minus= ~number;
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    357mag is offline Member
    Join Date
    May 2010
    Posts
    18
    Rep Power
    0

    Default

    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)?

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by 357mag View Post
    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)?
    I doesn't work like that (see the little table in my previous reply); to negate a number in the twos-complement system you: flip all bits and add 1 to the result.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    357mag is offline Member
    Join Date
    May 2010
    Posts
    18
    Rep Power
    0

    Default

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

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by 357mag View Post
    What do you mean it doesn't work like that? So then what Junky wrote is wrong?
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    357mag is offline Member
    Join Date
    May 2010
    Posts
    18
    Rep Power
    0

    Default

    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.

  16. #16
    357mag is offline Member
    Join Date
    May 2010
    Posts
    18
    Rep Power
    0

    Default

    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 11:00 PM.

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by 357mag View Post
    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.
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    357mag is offline Member
    Join Date
    May 2010
    Posts
    18
    Rep Power
    0

    Default

    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.

  19. #19
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by 357mag View Post
    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.
    Yep, that's correct; I think you got it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •