In C, the integer (for 32 bit machine) is 32 bits, and it ranges from -32,768 to +32,767. In Java, the integer(long) is also 32 bits, but ranges from -2,147,483,648 to +2,147,483,647.
I do not understand how the range is different in Java, even though the number of bits is the same. Can someone explain this?
In C, the language itself does not determine the representation of certain datatypes. It can vary from machine to machine, on embedded systems the int
can be 16 bit wide, though usually it is 32 bit.
The only requirement is that short int
<= int
<= long int
by size. Also, there is a recommendation that int
should represent the native capacity of the processor.
All types are signed. The unsigned
modifier allows you to use the highest bit as part of the value (otherwise it is reserved for the sign bit).
Here's a short table of the possible values for the possible data types:
width minimum maximum
signed 8 bit -128 +127
signed 16 bit -32 768 +32 767
signed 32 bit -2 147 483 648 +2 147 483 647
signed 64 bit -9 223 372 036 854 775 808 +9 223 372 036 854 775 807
unsigned 8 bit 0 +255
unsigned 16 bit 0 +65 535
unsigned 32 bit 0 +4 294 967 295
unsigned 64 bit 0 +18 446 744 073 709 551 615
In Java, the Java Language Specification determines the representation of the data types.
The order is: byte
8 bits, short
16 bits, int
32 bits, long
64 bits. All of these types are signed, there are no unsigned versions. However, bit manipulations treat the numbers as they were unsigned (that is, handling all bits correctly).
The character data type char
is 16 bits wide, unsigned, and holds characters using UTF-16 encoding (however, it is possible to assign a char
an arbitrary unsigned 16 bit integer that represents an invalid character codepoint)
width minimum maximum
SIGNED
byte: 8 bit -128 +127
short: 16 bit -32 768 +32 767
int: 32 bit -2 147 483 648 +2 147 483 647
long: 64 bit -9 223 372 036 854 775 808 +9 223 372 036 854 775 807
UNSIGNED
char 16 bit 0 +65 535