-1

Java Datatypes like int,short,byte are two's complement integers ,as they menioned it in here . what information does it give when someone says that in java , int ,short or byte are two's complement integers ?

Update : i wanted to know why 2's complement is prefered over other representations ?

Alok Mishra
  • 926
  • 13
  • 39
  • It means they are represented in memory according to strict and unambiguous rules; Google for more details. – Bathsheba Jun 11 '15 at 07:48
  • You can find your answer here: http://stackoverflow.com/questions/1049722/what-is-2s-complement – Gondy Jun 11 '15 at 07:52
  • It's a way to represent numbers in a computer that works well for positive as well as negative numbers. See [Two's complement](http://en.wikipedia.org/wiki/Two%27s_complement) on Wikipedia for a detailed explanation. – Jesper Jun 11 '15 at 08:00
  • Also see [Why We Use Two's Complement](https://www.youtube.com/watch?v=lKTsv6iVxV4) (YouTube). – Jesper Jun 11 '15 at 08:02

1 Answers1

3

it tells you how signed values (+/-) are represented in binary form.

for example

24 in simple binary form is 00011000

  • one's complement is 11100111 (inverting all bits)
  • two's complement is computed by adding 1 to the one's complement value

--> 11101000 is the two's complement for -24

that's why (as an example) in java the range of a byte-value is -128 ... 127 all values having a '1' in the 2^7 position are negative.

André R.
  • 1,627
  • 10
  • 14