Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

binary numbers
numbers
integer

Binary representation of positive integers

Educative Answers Team

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.

svg viewer

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×2×2×2=82 \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:

20=12^0=1

21=22^1=2

22=42^2=4

23=82^3=8

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

3=2+1=21+203 = 2+1=2^1 +2^0

5=4+1=22+205 = 4+1=2^2 +2^0

10=8+2=23+2110 = 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 0th0^{th} power, the second from the right represents 1st1^{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+21+20=00113 = 0+0+2^1 +2^0 = 0011

5=0+22+0+20=01015 =0+ 2^2+ 0 +2^0 = 0101

10=23+0+21+0=101010 = 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?

A)

127

B)

128

C)

255

D)

256

Question 1 of 30 attempted

RELATED TAGS

binary numbers
numbers
integer
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring