6

quoting from oracle website "byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive)".

here, the first two lines are valid but the last is not

            byte b = -128;
        byte b1 = 127;
        byte b2 = b>>>b1;//illegal

Q1) what is meant exactly by 8-bit signed? 128 in binary format would be 1000 0000 and -128 would need an extra bit for the negative sign, where it would fit if all the 8 bits are occupied.

Q2) for int, there is a unsigned right shift operator, but that seems illegal with bytes, why is it so. can overflow not be prevented in case of bytes. In case of int, it works

Thanks for your help

Community
  • 1
  • 1
eagertoLearn
  • 9,772
  • 23
  • 80
  • 122

1 Answers1

5
  1. Just what it sounds like: There are 8 bits, holding 2^8 = 256 possible values. It's signed, so the range is frmo -128 through 127 (256 values). The most significant bit has a value of -128.

  2. In Java, binary numeric promotion occurs with operations such as b >>> b1. Both types are promoted to int, and the result is an int. However, you can explicitly cast the result back to byte.

Here's the cast:

byte b2 = (byte) (b >>> b1);

The JLS, Section 5.6.2, talks about binary numeric promotion:

Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules:

If either operand is of type double, the other is converted to double.

Otherwise, if either operand is of type float, the other is converted to float.

Otherwise, if either operand is of type long, the other is converted to long.

Otherwise, both operands are converted to type int.

(emphasis mine)

rgettman
  • 176,041
  • 30
  • 275
  • 357
  • thanks, in terms of 8-bits, where does the sign go? `+` and `-` should be part of 8-bit right? – eagertoLearn Oct 17 '13 at 18:30
  • Think of the value of the `byte` as the sum of the values of all 8 bits: -128, 64, 32, 16, 8, 4, 2, and/or 1. – rgettman Oct 17 '13 at 18:32
  • That means a 8-bit representation of 1000 0000 should be -128? and not 128.? – eagertoLearn Oct 17 '13 at 18:38
  • Right, `-128` is represented as `1000 0000`, and `127` is represented as `0111 1111`. That also means that `-1` is represented as `1111 1111`. – rgettman Oct 17 '13 at 18:40
  • thanks for clearing out. but since you mentioned `-1` is represented as 1111 1111`, I am confused there. please clear this out. Thank you! – eagertoLearn Oct 17 '13 at 18:46
  • 1
    '-128' is '1000 0000', from there you count back to zero like this: '-127' is '1000 0001', '-126' = '1000 0010', '-125' = '1000 0011', .. '-1' is '1111 1111', '0' is '0000 0000' – Stijn de Witt May 12 '14 at 22:39
  • There is one problem with the given answer. If `b` is negative (has its most significant bit set), the resultant byte will be filled with 1s in `b1` high-order places, because `b` is sign-extended to 32 bits before the shift takes place. To achieve the expected behavior of zero-filling high order bits, you need to do this: `byte b2 = (byte) ((b & 0xff) >>> b1);` (note that you can use `>>` interchangeably in this case because the sign bit is always 0). – David M. Lloyd Dec 11 '14 at 15:24
  • @DavidM.Lloyd That would effectively treat the `byte` `b` as unsigned, because if the shift amount `b1` is `1`, then the result is `64`, not `-64`. – rgettman Dec 11 '14 at 17:30