-1

I have been learning a bit about binary and memory and I looked how int is being written, and from what I understood it basically writes 4 bytes, where the max number for a signed is where your 4 bytes are 127:

    byte a = Byte.MAX_VALUE;
    byte b = Byte.MAX_VALUE;
    byte c = Byte.MAX_VALUE;
    byte d = Byte.MAX_VALUE;

    System.out.println((a << 24) + (b << 16) + (c << 8) + (d << 0));

And then I shift left by a byte (8 bits) every time).

But the maximum number i get is 2139062143 instead of 2147483647, why is that?

Ben Beri
  • 1,101
  • 4
  • 23
  • 64

3 Answers3

1

In binary, MAX_VALUEs for each type start with a 0 bit and continue with all 1 bits. That means, to get Integer.MAX_VALUE, you'll need a 0 bit followed by 31 1 bits:

Integer.MAX_VALUE:    01111111111111111111111111111111

However, you're not producing that. You're using Byte.MAX_VALUE repeated four times. This means you have a 0 bit followed by seven 1 bits followed by a 0 bit followed by seven 1 bits and so on:

Byte.MAX_VALUE:       01111111
Byte.MAX_VALUE×4:     01111111011111110111111101111111 (2,139,062,143)

Since -1s are composed solely of 1 bits, you can get what you want with one Byte.MAX_VALUE followed by three -1s:

Byte.MAX_VALUE:       01111111
-1:                   11111111
Byte.MAX_VALUE, -1×3: 01111111111111111111111111111111

A second problem will arise when you try to combine these. When Java converts a negative byte to an int, it becomes a negative int, which means a whole lot of 1s are added to the left side of it in binary. Thus, you'll need to use & 0xff (or Byte.toUnsignedInt) to strip those added bits from each byte.

It's also better to use | instead of +, because it fits the meaning of what's going on more precisely.

So, to produce Integer.MAX_VALUE:

byte a = Byte.MAX_VALUE;
byte b = -1;
byte c = -1;
byte d = -1;

System.out.println(((a & 0xff) << 24) | ((b & 0xff) << 16) | ((c & 0xff) << 8) | ((d & 0xff) << 0));
Chai T. Rex
  • 2,972
  • 1
  • 15
  • 33
  • Got it. -1 & 0xff gives me 255, i need 127,255,255,255 in order to create 01111111111111111111111111111111 which the first bit represents if the signed number is negative or positive where if it was 1 it would be negative. Am i right? – Ben Beri Mar 03 '17 at 11:19
  • But what if I want to do it negative? – Ben Beri Mar 03 '17 at 11:23
  • Yes. Which negative value? – Chai T. Rex Mar 03 '17 at 19:16
  • If i want negative max value of singed integer, I have to follow the 2's complement which is negative_value = ~value+1 according to http://stackoverflow.com/a/13422442/1609248, how'd i do it in this case? – Ben Beri Mar 04 '17 at 14:52
  • `MIN_VALUE`s are one `1` followed by all `0`s. So, you'd use `Byte.MIN_VALUE` followed by three `0` bytes. – Chai T. Rex Mar 04 '17 at 16:57
0

No, the MSB must be zero, all other bits need to be 1. This gives you 0x7FFFFFFF, the maximum value for a signed 32-bit integer in 2's complement.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
0

byte is signed between -128 and 127. So -1 are 8 ones.

As shifting << is done after promoting byte to int, you would get an int -1 of 32 ones.

So do

int a = 0xFF; // Or 0b1111_1111
int b = 0xFF;
int c = 0xFF;
int d = 0xFF;

Or do:

byte[] bytes = { (byte)-1, (byte)-1, (byte)-1, (byte)-1 };
int n = ByteBuffer.wrap(bytes).getInt();

All ones one can get as:

int n = -1;
int n = ~0; // Ones complement
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • http://stackoverflow.com/questions/2840190/java-convert-4-bytes-to-int but I did exactly like in this answer, except the OR and AND operators, theres something I am misunderstanding? Shouldnt 4 bytes with value 127 give you signed integer max value which is 2147483647 – Ben Beri Mar 02 '17 at 20:38
  • 127 is binary 0111_111, Byte.MAX_VALUE. So no. You can shift a byte (sign extended to an int by the shift op) as: `(b & 0xFF) << ...`. – Joop Eggen Mar 02 '17 at 21:01