int32 storage in memory [duplicate]
Asked Answered
D

1

3

I have a question about int32 storage (c#).

32 bits means that the biggest number for int is 2^32.

2^32 = 4294967296, if you divide it by 2 you get the maximum value for an int32 :

4294967296 / 2 = -2147483648 to 2147483648

So I thought half of the bits are for negative figures and the other for positive. But that cant be true because 2^16 = 65536.

Now my question :

How is this actually set up in memory?

Im really curious about your answers.

Deirdredeism answered 31/3, 2014 at 11:11 Comment(4)
en.wikipedia.org/wiki/Two%27s_complementCarafe
Pretty well formed question.. But presius is right. Basically since integers can hold negative values one of its bits in memory must be used to allow -1 and 1 to both exist (for example)Imperial
unsurprisingly, this has come up before.Ceratoid
You only need ONE bit for the sign, so there are 31 bits left for the representation of the actual number. The range is -(2^31) to 2^31-1Partnership
B
8

Only one bit is used for sign (negative or positive)

Int32 values are represented in 31 bits, with the thirty-second bit used as a sign bit. Positive values are represented by using sign-and-magnitude representation. Negative values are in two's complement representation, MSDN

Int32.MaxValue =  2^31 - 1 = 01111111111111111111111111111111                
Int32.MinValue = -2^31     = 10000000000000000000000000000000

I have found nice article to understand the two's complement here.

Conversion from Two's Complement

Use the number 0xFFFFFFFF as an example. In binary, that is:

1111 1111 1111 1111 1111 1111 1111 1111

What can we say about this number? It's first (leftmost) bit is 1, which means that this represents a number that is negative. That's just the way that things are in two's complement: a leading 1 means the number is negative, a leading 0 means the number is 0 or positive.

To see what this number is a negative of, we reverse the sign of this number. But how to do that? To reverse the sign you simply invert the bits (0 goes to 1, and 1 to 0) and add one to the resulting number.

The inversion of that binary number is, obviously:

0000 0000 0000 0000 0000 0000 0000 0000

Then we add one.

0000 0000 0000 0000 0000 0000 0000 0001

So the negative of 0xFFFFFFFF is 0x00000001, more commonly known as 1. So 0xFFFFFFFF is -1.

Conversion to Two's Complement

Note that this works both ways. If you have -30, and want to represent it in 2's complement, you take the binary representation of 30:

0000 0000 0000 0000 0000 0000 0001 1110

Invert the digits.

1111 1111 1111 1111 1111 1111 1110 0001

And add one.

1111 1111 1111 1111 1111 1111 1110 0010

Converted back into hex, this is 0xFFFFFFE2

Edit, How does CPU performs subtraction using Two's complement

The CPU performs the subtraction using addition on two's complement of negative number. Lets take an example of 8 bit numbers. We want to subtract 4 from seven.

7 = 00000111
4 = 00000100
Two' complement of 4

Step 1 take inverse of 00000100 by converting 0 to 1 and 1 to 0

00000100 -> 11111011

Step 2 Add one to inverse

11111011
00000001
========
11111100

7- 4 = 7 + (Two's complement of 4)

00000111 (binary representation of 7)
11111100 (binary representation after Two's complement of 4)
========
00000011  (binary representation of 3)    
Bargeboard answered 31/3, 2014 at 11:13 Comment(3)
thanks for the good answer. but why is this so "complicated" ? since there is a sign bit why cant it be like that: value 2 = 0000 0000 0000 0000 0000 0000 0000 0010 value -2= 1000 0000 0000 0000 0000 0000 0000 0010 i guess the reason why it cant be this way is the way the compiler calculates bit values, am i right?Deirdredeism
Its explained very well here, https://mcmap.net/q/1176742/-how-does-the-cpu-do-subtractionBargeboard
Check my updated answer, I have added an example showing how CPU using Two's complement to perform subtraction using addition.Bargeboard

© 2022 - 2024 — McMap. All rights reserved.