Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is an integer stored in memory?

This is most probably the dumbest question anyone would ask, but regardless I hope I will find a clear answer for this.

My question is - How is an integer stored in computer memory?

In c# an integer is of size 32 bit. MSDN says we can store numbers from -2,147,483,648 to 2,147,483,647 inside an integer variable.

As per my understanding a bit can store only 2 values i.e 0 & 1. If I can store only 0 or 1 in a bit, how will I be able to store numbers 2 to 9 inside a bit?

More precisely, say I have this code int x = 5; How will this be represented in memory or in other words how is 5 converted into 0's and 1's, and what is the convention behind it?

like image 578
Mike Avatar asked Aug 29 '13 18:08

Mike


People also ask

How are numbers stored in memory?

Numbers are stored on the computer in binary form. In other words, information is encoded as a sequence of 1's and 0's. On most computers, the memory is organized into 8-bit bytes. This means each 8-bit byte stored in memory will have a separate address.

How are integers stored in memory Java?

Specifically, Java stores it using 32 bits of memory. In other words, it can represent values from -2,147,483,648 (-231) to 2,147,483,647 (231-1). In Java 8, it's possible to store an unsigned integer value up to 4,294,967,295 (232-1) by using new special helper functions.

What is the memory of integer?

Memory Integers are 16-bit integer operands that may be signed or unsigned. The range of an MI is -32768 to +32767. There are 2048 MIs (Address MI 0 - MI 2047).

How are real numbers and integers stored in a computer?

In general, computers store real numbers in Scientific Notation, or Floating Point Format. That means that instead of storing the binary number 1010.1101 as it is written on the screen right now, the computer may represent it internally as 0.10101101*24.


2 Answers

It's represented in binary (base 2). Read more about number bases. In base 2 you only need 2 different symbols to represent a number. We usually use the symbols 0 and 1. In our usual base we use 10 different symbols to represent all the numbers, 0, 1, 2, ... 8, and 9.

For comparison, think about a number that doesn't fit in our usual system. Like 14. We don't have a symbol for 14, so how to we represent it? Easy, we just combine two of our symbols 1 and 4. 14 in base 10 means 1*10^1 + 4*10^0.

1110 in base 2 (binary) means 1*2^3 + 1*2^2 + 1*2^1 + 0*2^0 = 8 + 4 + 2 + 0 = 14. So despite not having enough symbols in either base to represent 14 with a single symbol, we can still represent it in both bases.

In another commonly used base, base 16, which is also known as hexadecimal, we have enough symbols to represent 14 using only one of them. You'll usually see 14 written using the symbol e in hexadecimal.

For negative integers we use a convenient representation called twos-complement which is the complement (all 1s flipped to 0 and all 0s flipped to 1s) with one added to it.

There are two main reasons this is so convenient:

  • We know immediately if a number is positive of negative by looking at a single bit, the most significant bit out of the 32 we use.

  • It's mathematically correct in that x - y = x + -y using regular addition the same way you learnt in grade school. This means that processors don't need to do anything special to implement subtraction if they already have addition. They can simply find the twos-complement of y (recall, flip the bits and add one) and then add x and y using the addition circuit they already have, rather than having a special circuit for subtraction.

like image 175
Paul Avatar answered Oct 04 '22 18:10

Paul


This is not a dumb question at all.

Let's start with uint because it's slightly easier. The convention is:

  • You have 32 bits in a uint. Each bit is assigned a number ranging from 0 to 31. By convention the rightmost bit is 0 and the leftmost bit is 31.
  • Take each bit number and raise 2 to that power, and then multiply it by the value of the bit. So if bit number three is one, that's 1 x 23. If bit number twelve is zero, that's 0 x 212.
  • Add up all those numbers. That's the value.

So five would be 00000000000000000000000000000101, because 5 = 1 x 20 + 0 x 21 + 1 x 22 + ... the rest are all zero.

That's a uint. The convention for ints is:

  • Compute the value as a uint.
  • If the value is greater than or equal to 0 and strictly less than 231 then you're done. The int and uint values are the same.
  • Otherwise, subtract 232 from the uint value and that's the int value.

This might seem like an odd convention. We use it because it turns out that it is easy to build chips that perform arithmetic in this format extremely quickly.

like image 38
Eric Lippert Avatar answered Oct 04 '22 19:10

Eric Lippert