-1

since english isn't my main language and i've didn't find any pointers as to how to manually calculate the new number.

Example:

byte b = (byte) 720; 
System.out.println(b); // -48

I know that most primary data types use the two complement. Byte value ranges from -128 to 127, so it's expected to round the number down to something that fits in byte.

The Question is how do i manually calculate the -48 of the typecasted 720? I've read that i have to convert it to two complement, so taking the binary number, searching from right to left for the first one and then inverting all others and since byte only has 8 bits, take the first 8 bits. But that didn't quite work for me, it would be helpful if you would tell me how to calculate a typecasted number that doesn't fit into byte. Thanks for reading! :)

Alexis C.
  • 91,686
  • 21
  • 171
  • 177
  • Not sure if Java follows the same conventions but most compilers don't explicitly guarantee that that will be the case, if you assume two's complement then you can just look at the lower byte of the value and that should be the representation. – Jesus Ramos Jan 06 '14 at 22:02

4 Answers4

4

The binary representation of 702 is

10 1101 0000

Get rid of everything except the last 8 bits (because that's what fits into a byte).

1101 0000

Because of the leading 1, get the complement

0010 1111

and add 1

0011 0000

and negate the value. Gives -48.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
3

In Java, integral types are always 2's complement. Section 4.2 of the JLS states:

The integral types are byte, short, int, and long, whose values are 8-bit, 16-bit, 32-bit and 64-bit signed two's-complement integers

You can mask out the least significant 8 bits.

int value = 720;
value &= 0xFF;

But that leaves a number in the range 0-255. Numbers higher than 127 represent negative numbers for bytes.

if (value > Byte.MAX_VALUE)
    value -= 1 << 8;

This manually yields the -48 that the (byte) cast yields.

rgettman
  • 176,041
  • 30
  • 275
  • 357
  • Could you get from the -48 back to the original 720? I was able to add back the 256 to get to 208, but unable to find a way to get back to the 720. – luckyguy73 Nov 07 '20 at 18:31
  • @luckyguy73 Once you've masked out the upper bits, you have destroyed part of the data that make up the number `720`. There is no more knowledge that the original number was `720`, unless you backed up the value first. You can keep adding 256, and you'll get back to 720 eventually (and beyond), but there is no longer any knowledge that you had 720 originally. You could use `208`, `464`, `720`, or any number that is equivalent to 208 modulo 256 to get `-48`. – rgettman Nov 10 '20 at 00:34
1

First what happens is the value (in binary) is truncated.

720 binary is 0b1011010000

We can only fit 8 bits 0b11010000

first digit 1, so the value is negative.

2's compliment gives you -48.

SpacePrez
  • 1,086
  • 7
  • 15
0

This is close to redundant with the answer posted by rgettman, but since you inquired about the two's complement, here is "manually" taking the 2's complement, rather than simply subtracting 256 as in that answer.

To mask the integer down to the bits that will be considered for a byte, combine it bitwise with 11111111.

  int i = 720;
  int x = i & 0xFF;

Then to take the two's complement:

  if (x >> 7 == 1) {
    x = -1 * ((x ^ 0xFF) + 1);
  }
  System.out.println(x);
Affe
  • 47,174
  • 11
  • 83
  • 83