Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

binary numbers
numbers
integer

# Binary representation of positive integers

Computers innately consist of transistors that take two forms: on or off. You may be thinking, “How can a computer do something as basic as representing a number using these transistors?”

## 1 and 0

Let’s take the first small step and use these transistors to represent 1 and 0. We can perceive a transistor in an on state to be 1 and a transistor in an off state can be considered 0. We call this single unit of information a bit. A bit can take a value of either 0 or 1.

The following example shows this using bulbs.

## Bigger numbers

Now, we can use multiple bits together to represent bigger numbers as well. For example, if we combine 4 bits together, we will have 16 unique combinations. This is because each bit can take two values (0 or 1), so four bits together means $2 \times 2\times 2\times 2 = 8$. We can label each of these 16 combinations with numbers from 0 to 15. Thus. we can represent 16 unique numbers with just 4 bits.

Now, the question arises, “How do we label these combinations systematically?” Turns out using power’s of two systematically give us a nice rule for labeling as well. Some powers of two are:

$2^0=1$

$2^1=2$

$2^2=4$

$2^3=8$

Now the cool thing is that you can make any positive integer by adding powers of two. For example:

$3 = 2+1=2^1 +2^0$

$5 = 4+1=2^2 +2^0$

$10 = 8+2=2^3 +2^1$

If you have 4 bits now, you can imagine that those 4 bits represent a power of 2. The rightmost bit would represent the $0^{th}$ power, the second from the right represents $1^{st}$ power, and so on. Any of these 4 bits being 0 or 1 represent whether that two’s power contributes to the sum for that number or not. For example, if the first bit from the right is 0, then the number does not need the first power of two in its sum. This way, we can represent the numbers from the above example using 4 bits as:

$3 = 0+0+2^1 +2^0 = 0011$

$5 =0+ 2^2+ 0 +2^0 = 0101$

$10 = 2^3+0 +2^1+0 = 1010$

Here, since two’s first two powers are contributing to 3, we have the two rightmost bits’ 1 in 3 binary representation. This explanation hold for the other two examples too.

## An interactive simulation

Given below is an interactive simulation for an 8-bit representation of a number. Continuing with our example of bulbs, a lit-up bulb represents that the power corresponding to that bit contributes to the number, while a dimmed out bulb represents that it does not.

Play around with the simulation and then answer the questions at the bottom to test your learning.

A binary number visualization
1

What is the maximum possible number that can be represented using 8 bits?

127

128

255

###### D)

256

Question 1 of 30 attempted

RELATED TAGS

binary numbers
numbers
integer