max value of integer

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?

Solutions

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

In C, the integer(for 32 bit machine) is 32 bit and it ranges from -32768 to +32767.

Wrong. 32-bit signed integer in 2's complement representation has the range -231 to 231-1 which is equal to -2,147,483,648 to 2,147,483,647.

A 32 bit integer ranges from -2,147,483,648 to 2,147,483,647. However the fact that you are on a 32-bit machine does not mean your C compiler uses 32-bit integers.

Tags: Java / C / Integer

Similar questions

leftOuterJoin JavaPairRDD<Integer, Integer> and JavaPairRDD<Integer, Map<Integer, Integer>>
I am trying to perform leftOuterJoin of JavaPairRDD and JavaPairRDD> and in function signature return type is Optional here is com.google.common.base.Optional Is this the correct return type when I perform leftOuterJoin? My IDE is giving this error I couldn't find proper documentation for this. If there are any links to understand this better that ...
Why in this if statement `if (myList[i] > max) max = myList[i];` the `max` is kept outside the brackets?
In the following code for finding largest number in an array, why is the max put outside the code? Why is the max outside the brackets? In this line, I am unable to understand the construct of the if statement.
How to Find the Max Integer Value in a Stack without using max() or iterating over it?
I was asked in an interview the following question: if you have a Stack of Integers how would you find the max value of the Stack without using Collections.max and without iterating over the Stack and comparing elements. I answered it with the below code as I don't know of another way than using any Collections API or iterating over the Stack and u...
Difference between @size(max = value ) and @min(value) and @max(value)
I want to do some domain validation. In my object I have one integer. Now my question is: if I write and If it's an integer which one is proper for domain validation? Can anybody explain me what is the difference between them? Thanks.
The method max(Comparator<? super List<Integer>>) in the type Stream<List<Integer>> is not applicable for the arguments (Comparator<Integer>)
I am trying to fetch the student name based on his maximum marks using java8 streams API. I would like to return the "Rohit" in output because he scores max 100 marks. whether it is possible to achieve in a single stream operation, compare all the marks and return Thanks in Advance and appreciating your help.
Java 8 Lambdas max() on stream using Integer.max as comparator
I have written simple List with example values, and I wanted the stream to return max value from Stream. I know max() function takes Comparator but it turned out, I can pass also Integer::max (anyone can explain my, why?). Moreover, program prints weird result, I inspected it "inside" and it looks OK, but after I get final result - they are not acc...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.