11

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?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Mike
  • 3,204
  • 8
  • 47
  • 74
  • As a clue, you might want to search for "how does binary work", "how to convert decimal to binary", etc.. :-) – John Parker Aug 29 '13 at 18:08
  • 1
    possible duplicate of [What is “2's Complement”?](http://stackoverflow.com/questions/1049722/what-is-2s-complement) – John Kugelman Aug 29 '13 at 18:25
  • 1
    The numbers we use every day can only be 0 through 9, yet we're able to represent larger numbers. You just did it in your post! It's all about where the digit is in relation to the decimal. – Steve Aug 29 '13 at 18:41

4 Answers4

14

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.

Paul
  • 139,544
  • 27
  • 275
  • 264
12

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.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • In other words, uints use base 2. Contrast this with base 10, where one would represent 205 as 205, because 205 = `2*10^2 + 0*10^1 + 5*10^0` – Brian Aug 30 '13 at 15:13
4

Binary works as follows (as your 32 bits).

   1  1  1  1 | 1  1  1  1 | 1  1  1  1 | 1  1  1  1 | 1 1 1 1 | 1 1 1 1 | 1 1 1 1 | 1 1 1 1

2^ 31 30 29 28  27 26 25 24  23 22 21 20  19 18 17 16......................................0
   x

x = sign bit (if 1 then negative number if 0 then positive)

So the highest number is 0111111111............1 (all ones except the negative bit), which is 2^30 + 2 ^29 + 2^28 +........+2^1 + 2^0 or 2,147,483,647.

The lowest is 1000000.........0, meaning -2^31 or -2147483648.

Tricky12
  • 6,752
  • 1
  • 27
  • 35
  • Two's complement doesn't have a sign bit. Zero has no sign, for example. – Joey Aug 29 '13 at 18:17
  • 1
    It definitely does have a sign bit. Here is a SO question discussing how it works: http://stackoverflow.com/questions/1049722/what-is-2s-complement. – Tricky12 Aug 29 '13 at 18:21
  • 1
    Regarding the *sign bit* question... it depends on your definitions. Signed integers in both 1's-complement and 2's-complement have a bit that you can test to see if the number is negative, and that is often referred to as the *sign bit* for the representation. Some definitions however require that you be able to negate a number by simply flipping the sign bit... which you certainly can't do with either 1's- or 2's-complement representation. – Corey Oct 01 '13 at 07:42
1

Is this what high level languages lead to!? Eeek!

As other people have said it's a base 2 counting system. Humans are naturally base 10 counters mostly, though time for some reason is base 60, and 6 x 9 = 42 in base 13. Alan Turing was apparently adept at base 17 mental arithmetic.

Computers operate in base 2 because it's easy for the electronics to be either on or off - representing 1 and 0 which is all you need for base 2. You could build the electronics in such a way that it was on, off or somewhere in between. That'd be 3 states, allowing you to do tertiary math (as opposed to binary math). However the reliability is reduced because it's harder to tell the difference between those three states, and the electronics is much more complicated. Even more levels leads to worse reliability.

Despite that it is done in multi level cell flash memory. In these each memory cell represents on, off and a number of intermediate values. This improves the capacity (each cell can store several bits), but it is bad news for reliability. This sort of chip is used in solid state drives, and these operate on the very edge of total unreliability in order to maximise capacity.

bazza
  • 7,580
  • 15
  • 22
  • 15
    Leading with a pejorative comment is unnecessary. There was a day when you didn't know this, and a day later you did know it. Today is Mike's day. – Eric Lippert Aug 29 '13 at 18:43
  • Fun fact: the origins of the common use of base 60 in certain areas is debated, but one major origin is Babylonian mathematics, in which counting notation used a combination of base 10 and 60, counting by tens but ending at 60 rather than 100 and using that as a unit for higher figures. – UpQuark Aug 29 '13 at 18:45
  • And yes, this is what high level languages lead to. I could also point out that *not having to know what voltage level in a flip flop circuit represents a one* is what languages lead to. Do you feel that you're missing out by not knowing what the voltage levels in your flip flops are? – Eric Lippert Aug 29 '13 at 18:51
  • @EricLippert, well, lets see. For a lot of programmers it makes not the blindest bit of difference. The problem the industry has is that there's quite a lot of programming that requires good knowledge of the electronic consequences (and thence power and thermal; these really matter to the likes of Google, etc) of the source code. For instance, how many programmers out there know what memory allocation actually is any why you might in quite a lot of circumstances want to avoid it if at all possible? Understand your hardware so that when it matters you can write useful code. – bazza Aug 29 '13 at 19:26
  • @EricLippert; Also, I date from an age when you had to know this stuff before you could even get something to TTY "Hello, World". Everything we learnt back then about "What A Computer Actually Is" is of immense value to anyone with ambitions to upscale an operation. Plus, being an old fogey I find it surprising that someone can get as far as C# without knowing some basic stuff. – bazza Aug 29 '13 at 19:31