Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why has the int32 type a maximum value of 2³¹ − 1? [duplicate]

Tags:

Possible Duplicate:
What is "2's Complement"?

I know int32 has a length of 32 bits (4 bytes). I assume it has 2³² values but as half of them must be under zero, I guess it has something to do with this. I would like to know why exactly int32 has max. positive number 2³¹ − 1.

like image 811
Petr Avatar asked Sep 29 '10 23:09

Petr


People also ask

What is the maximum value for an Int32?

Remarks. The value of this constant is 2,147,483,647; that is, hexadecimal 0x7FFFFFFF.

Why is 2,147,483,647 the max int value?

The number 2,147,483,647 (or hexadecimal 7FFFFFFF16) is the maximum positive value for a 32-bit signed binary integer in computing. It is therefore the maximum value for variables declared as integers (e.g., as int ) in many programming languages.

How many values is Int32?

Int32 is an immutable value type that represents signed integers with values that range from negative 2,147,483,648 (which is represented by the Int32. MinValue constant) through positive 2,147,483,647 (which is represented by the Int32. MaxValue constant. .

What is the maximum value that a signed integer variable can have in 32-bit compiler?

A 32-bit integer limit allows for 4,294,967,296 (232 2 3 2 ) pieces of data. If storing signed integers, this would range from -2,147,483,648 to 2,147,483,647.


2 Answers

This most significant bit is used to code the sign (1 meaning negative), so only 31 bits are available for the actual value.

Int32.MaxValue =  2^31 - 1 = 01111111111111111111111111111111
                  1        = 00000000000000000000000000000001
                  0        = 00000000000000000000000000000000
                 -1        = 11111111111111111111111111111111
Int32.MinValue = -2^31     = 10000000000000000000000000000000
like image 73
Thomas Levesque Avatar answered Sep 17 '22 14:09

Thomas Levesque


2³² possible values

− 2³¹ values used for negative integers

− 1 value used for zero

= 2³¹ − 1 values available for positive integers

like image 32
dan04 Avatar answered Sep 18 '22 14:09

dan04