/
Basics of how binary counting works

Basics of how binary counting works

Try and remember base number theory from school. In binary we have only two digits - 1 or 0.

So how do we represent a sequence like 1, 2, 3,4,5,6,7,8,9, 10 in binary?

This is what is looks like:

1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010

1, 2, 3, 4, 5, 6, 7, 8, 9, 10

From decimal we think of the “one’s column” and the “ten’s column”, every time we add one to 9 we carry the one and add it to the ten’s column.

In binary we only have two digits so the columns are:

  • First column - 1’s

  • 2nd column. - 2’s

  • 3rd column. - 4’s

  • 4th column - 8’s

  • i.e. going up by “powers of 2” - 2^(Nth column)

In binary we describe each binary digit as a “bit” - 0 or 1

A 32 bit number - is a binary number with 32 digits

The maximum size number we can store in a binary 32 bit number is 2^32 - 1 or in long hand
1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 - which is 4294967296

This is unsigned 32 bit integer in binary.

So how can we express negative numbers in binary? The convention is to have a signed integer and to use the left most “bit” - i.e. a digit to indicate whether or not the number is negative or positive.

If the top bit is 1 then the number is negative. A signed integer loses one of it’s digits and so it cannot express as big of a number as an unsigned integer.

The range is from -2^31 to 2^31 or -2147483648 to 2147483648

The interesting thing is 0 in signed integer looks like this - which is same as 0 in a unsigned integer

0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000

When we subtract 1 we end up with this:

1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111

Which for a signed integer means -1 in decimal

But for an unsigned integer it means 4294967296.



Related content

How do computers represent negative numbers?
How do computers represent negative numbers?
More like this
How does binary work?
How does binary work?
More like this
What are bits, bytes and 32 bit and 64 bit numbers in computers?
What are bits, bytes and 32 bit and 64 bit numbers in computers?
More like this
Unsigned integers don't protect you as much as you would think.
Unsigned integers don't protect you as much as you would think.
More like this
What is a signed versus unsigned integer?
What is a signed versus unsigned integer?
More like this
Integer Overflow Errors
Integer Overflow Errors
More like this