You have probably noticed that IT people are a little strange. They discuss alarming subjects (I know of a tram passenger who took offense when students started talking about Java), use technical jargon, and even count differently. Ask one for 100 zł, and they round it up to 128 zł.

On a more serious note, today we will focus on the number system used in computing: the binary system. Most people know that computing uses zeros and ones and successive powers of 2. Not everyone understands why, what the consequences are, or how to switch between the decimal system, natural to people, and binary. It is not particularly difficult. Understanding it helps explain calculations and even jokes such as "there are 10 types of people in the world." Once those twos make sense, it is also a short step to another way of writing numbers: the base-16 system, also known as hexadecimal, or "hex" for short.

Today, we will explain how to work with powers of two. I will try to use plain common sense, describing how I understand and use them myself. Acquiring practical knowledge and skills matters more than elegantly reciting a definition from a book.

What is binary?

As the name suggests, this way of counting uses only two digits: 0 and 1. Or "false" and "true," since a two-state value is closely related to whether a bit is on or off. But wait: what are bits and bytes?

You probably know that computers process information as bits: individual, indivisible pieces of data that can be on or off. Imagine a light bulb switched on or off. Depending on its state, a program may react differently, interpret a value as another number, or execute a different processor instruction. One bit stores very little information, of course, so we use groups of bits. Eight bits make one byte, which in practice is the smallest unit of information used by an information system. A byte can therefore have eight "lit light bulbs," but that does not mean its value is 8. That would be too simple. A byte can store a maximum value of 255. Today, we will explain why.

One byte still holds very little information. Computers therefore process huge numbers of them and combine them into units with prefixes such as "kilo," "mega," and "giga." That gives us kilobytes, megabytes, gigabytes, terabytes, and so on, whether we are discussing RAM, hard drives, or data sent over a network.

This system is less intuitive for people but much better suited to electronics, which relies on the presence or absence of a charge or a flow of current at a particular moment. Technologically, it is much more complicated, and I have no intention of going into those details. To be honest, programmers rarely go down to such a low electronic level and may not fully understand how everything works at the transistor level. Those working in low-level development are an exception, since this is their daily bread. Calculating in binary, however, is a skill most IT specialists should acquire, even if they do not use it every day.

How do you read a binary number?

A binary number is written as a sequence of bits read from left to right. We say that the most significant bit, the one with the greatest weight, is on the left. Decimal works similarly: in 67, we know that the "6" has more weight because it represents tens, while the "7" is "only" units. This becomes clear when we show how those values are calculated, something we usually do automatically in our heads:

67(10) → 6 * 10 + 7 * 1 = 67

We can also write this using powers of 10:

67(10) → 6 * 101 + 7 * 100 = 67

What we call units, tens, hundreds, and so on are mathematically successive powers of a particular number, starting at 0. As we know, 100 is 1. Here, the base is 10, hence the name decimal. Replacing 10 with 8 gives us octal; using 3 gives us ternary; and using 2 gives us binary. Let's write 67 that way and see how it translates back into "tens."

1000011(2) → 1 * 26 + 0 * 25 + 0 * 24 + 0 * 23 + 0 * 22 + 1 * 21 + 1 * 20 = 67(10)

We have simply expressed the same number differently, in binary. Instead of successive powers of 10, meaning 1, 10, 100, 1,000, and so on, we used powers of 2, giving the sequence 1, 2, 4, 8, 16, 32, and onward. The notation is consequently longer: here, we need 7 digits instead of 2. A computer can read the number 67 in this form, however, and it fits into one byte. Remember that a byte holds up to 8 bits, and here we have 7.

What would this look like in octal, for example?

103(8) → 1 * 82 + 0 * 81 + 3 * 80 = 67(10)

Things are slightly more complicated here because each position can hold a digit from 0 to 7, rather than just 0 or 1. An observant reader will see why: in base eight, each symbol can be one of eight digits, in this case 0 through 7. In decimal, it can be one of 0 through 9. Binary is convenient for calculations because there are only two possibilities: 0 or 1.

What is the maximum value a byte can hold?

Let's return to an earlier point: a byte can store a maximum value of 255. You may wonder why, since that is not a power of two. It is not even an even number. But 256 is, so why is that not the maximum? The answer lies in another basic computing principle: we count from 0, not 1. Yes, from zero.

A byte consists of eight bits, and we obtain its maximum value when every bit is 1, except when dealing with negative numbers, which comes much later. Let's break that value down:

11111111(2) → 1 * 27 + 1 * 26 + 1 * 25 + 1 * 24 + 1 * 23 + 1 * 22 + 1 * 21 + 1 * 20 = 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255(10)

The maximum is therefore 255 because putting 1 in every bit gives exactly that sum. Nothing more can be added within a single byte. To get 256, we would need a ninth bit set to 1, filling the positions to its right with zeros, and would therefore need a second byte. Zero fits perfectly well in one byte: just set all its bits to zero.

A byte can hold 256 different values, from 0 to 255, but the highest number is consequently 1 less. More generally, the maximum value stored in n bits is 2n - 1. For three bits, for example, the maximum is 23 - 1 = 7. Write down three ones and calculate the value to check.

Since numbers written in different bases are easily confused, you will notice that I add (n) beside individual values, where n is the base of the number system used. This makes conversions easier to follow.

How do you list consecutive binary numbers?

Before going further, we should mention that listing consecutive binary numbers is a fairly common task. Decimal is straightforward because it comes naturally to us: 1, 2, 3, 4, 5, and so on. What does that look like in binary?

  1. Decide the range you need, meaning how many bits to write.
  2. Start at zero, with every bit set to zero.
  3. Alternate 0 and 1 in the last, least significant bit, on the far right, changing with every number.
  4. In the second bit from the right, alternate 0 and 1 every two numbers.
  5. In the third bit from the right, alternate 0 and 1 every four numbers. The switching interval also comes from a power of two.
  6. Continue in this way up to the most significant bit.

That may sound complicated, but this three-bit example should make it clear:

000
001
010
011
100
101
110
111

How do you convert between decimal and binary?

We have shown how to convert a binary number into the "human" decimal system. In jokes about "10 types of people," the "10" should therefore be read in binary: 1 * 21 + 0 * 20 = 2. How do we convert in the opposite direction, into binary?

The main thing is to memorize successive powers of 2, or write them in the margin. In order, they are:

20 = 1
21 = 2
22 = 4
23 = 8
24 = 16
25 = 32
26 = 64
27 = 128
28 = 256

An example is the easiest way to explain the method. Take 198(10). First, find the highest power of two that is less than or equal to our number. Checking them in turn, we find 128, which is less than 198, while the next power, 256, is greater. Since 128 is the seventh power, 198 in binary will have 8 bits. Remember the zeroth power. Let's write it down:

1_______
198 - 128 = 70

We also subtract the value of that bit from our number: 198 minus 128 is 70. Now repeat the check for 70 at the following bit positions. Bit six has a value of 64, which is less than 70, so this bit also receives a 1. We extend our result:

11______
70 - 64 = 6

With 6, things change. The next exponent is 5, but 25 is 32, which is greater than 6. This bit therefore receives 0:

110_____

The same applies to bits four and three, with values of 16 and 8 respectively:

11000___

Bit two has a value of 4, which is less than our 6, so it receives 1. The same applies to the next bit, bit one, meaning the second from the right, because 2 ≤ 6 - 4.

110001__
6 - 4 = 2

1100011_
2 - 2 = 0

We now have 0 left, so the final bit will also be zero. Our result is 11000110(2). Converting it back into decimal should give our original 198(10). Let's check.

1 * 27 + 1 * 26 + 0 * 25 + 0 * 24 + 0 * 23 + 1 * 22 + 1 * 21 + 0 * 20 = 128 + 64 + 0 + 0 + 0 + 4 + 2 + 0 = 198(10)

The algorithm may seem a little complicated at first, but work through the example slowly and write it out on paper, and it will soon make sense. We identify the most significant bit, then move right one bit at a time, checking whether its value is less than or equal to the current remainder. If so, that bit receives 1; otherwise, it receives 0.

If we are converting a power of 2, such as 32, we can immediately see that its binary representation has just one bit set, with zeros filling the positions to the right. Here, that gives 100000(2).

The hexadecimal system

I mentioned that the base-16, or hexadecimal, system is closely related to binary. Its base is 16 rather than 2, and 16 is itself a power of two. These ways of writing numerical values are therefore connected, just as you might suspect.

Having sixteen digits means adding the first letters of the alphabet to the usual 0-9 range: A(16) is 10(10), B(16) is 11(10), C(16) is 12(10), D(16) is 13(10), E(16) is 14(10), and F(16) is 15(10).

How does this relate to binary? Let's use the method described earlier to list all combinations and convert them to hexadecimal:

0000(2) = 0(16)
0001(2) = 1(16)
0010(2) = 2(16)
0011(2) = 3(16)
0100(2) = 4(16)
0101(2) = 5(16)
0110(2) = 6(16)
0111(2) = 7(16)
1000(2) = 8(16)
1001(2) = 9(16)
1010(2) = A(16)
1011(2) = B(16)
1100(2) = C(16)
1101(2) = D(16)
1110(2) = E(16)
1111(2) = F(16)

We can therefore "compress" four bits into one character, but there is more. Because hexadecimal extends binary, this also works for larger numbers of bits, converted in groups of four. Again, an example will help.

Take a slightly larger number, 5643(10), which we can write in binary using 16 bits, or two bytes. I have added leading zeros for clarity:

0001011000001011

Now split it into groups of four bits:

0001 0110 0000 1011

Simply convert each group of four bits into its hexadecimal digit:

0001(2) = 1(16)
0110(2) = 6(16)
0000(2) = 0(16)
1011(2) = B(16)

Total: 160B

There we have it: 160B(16) is the hexadecimal equivalent of 5643(10). We can calculate it to make sure everything agrees:

1 * 163 + 6 * 162 + 0 * 161 + 11 * 160 = 4096 + 1536 + 11 = 5643

This much more convenient form is used by editors that let us inspect the contents of binary files. Below is an example from the VALORANT executable:

Fragment wnętrza pliku valorant.exe w postaci heksadecymalnej

Some characters have also been decoded into ordinary text. This is no coincidence. Text files consist of characters, each assigned a corresponding byte value from 0x00 to 0xFF. In the simplest case, one byte represents one character, as in ASCII. More modern uses, which must accommodate Polish letters, for example, require more bytes and an encoding such as UTF-8.

You may have noticed another unusual notation I used: 0xFF. The initial "0x" indicates a hexadecimal number, a format generally recognized by programming languages. Similarly, octal, also used in computing for purposes such as access permissions, takes a leading "0," as in 0713. The prefix "0b" often indicates a binary number, such as 0b101.

Variable types

What we have covered so far may explain why languages such as C++ have different variable types: the programmer should declare how many bytes to reserve for a variable. The larger the number to be stored, the more bytes it needs. That gives us the familiar types:

  • char: 1 byte
  • short: 2 bytes
  • int (integer): 4 bytes (32 bits)
  • long: 8 bytes (64 bits)

Not every programming language offers all these types, and byte counts do not always match those listed above. In newer programming paradigms, developers often leave type selection to the compiler or interpreter and need not worry about bytes. Still, it is useful to know that limits exist.

You might point out that a 2-byte short can therefore store a maximum decimal value of 65,535, and an integer can store 4,294,967,295. That is correct, provided we account for one small detail: these are unsigned versions of the types, meaning without a sign. What does that mean? We have forgotten that mathematics includes negative numbers. These types must accommodate them too. C++, for example, provides extra modifiers. For short, these are:

  • unsigned short: 2 bytes, from 0 to 65,535 (65,536 possible values)
  • signed short: 2 bytes, from -32,768 to 32,767 (still 65,536 possible values)

Although the total range remains unchanged, we apparently need to dedicate one bit to indicating whether a value is positive or negative. We will shortly explain how that works.

Operations on binary numbers

Binary numbers support mathematical operations, of course. Five are particularly common:

  • bitwise OR
  • bitwise AND
  • exclusive OR (XOR)
  • left bit shift
  • right bit shift

This article illustrates bitwise operations well. Let's also briefly discuss them here.

To understand the first two, we need to know, or recall, conjunction (AND) and disjunction (OR), two important logical operations. AND returns 1 only when both inputs are 1; otherwise, it returns 0. OR returns 0 only when both inputs are 0; in every other case, it returns 1.

Consider the bitwise OR of 1011(2) and 1001(2). First, make the numbers the same length by adding leading zeros, which is unnecessary here. Then check which positions contain a 1 in at least one number. We are applying OR to corresponding bits. The result is 1011(2), since only the second most significant position has no 1 in either input.

Bitwise AND works similarly, but with the opposite criterion: we apply conjunction at every position, looking for places where both bits are 1. There are two here: the first and last positions in both numbers. The result is therefore 1001(2).

The familiar XOR instead looks for corresponding bits with different values. Here, the result is 0010(2), or simply 10(2), because only the second least significant position differs.

Bit shifts mean exactly what they sound like: shifting bits one place left or right and, where needed, filling from the right with zeros. This resembles moving the decimal point when multiplying or dividing by ten in decimal, except that the factor here is 2. Shifting 1011(2), or 11(10), left gives 10110(2), or 22(10), twice 11. Shifting right gives 0101(2), or 5(10). That last case is interesting: 11 divided by 2 should be 5.5 rather than 5. Where did the half go? In its standard form, binary does not store fractions and rounds everything down. That does not mean a "nonstandard" way does not exist.

Negative and floating-point numbers

We have mentioned that binary can store negative numbers. Now it is time to explain how, though it is a little more complicated than you might expect.

There are three ways to represent numbers below zero, all based on dedicating the most significant bit to the sign. Put simply, if the first bit is 0, the number is positive, with no sign. If it is 1, the number is negative, with a sign. The remaining bits describe the number as usual, but with one fewer bit available, smaller values can be stored.

The simplest of these three representations is sign-magnitude, which works like the standard representation except for dedicating the first bit to the sign. Although straightforward for people, it was not efficient enough for computers in terms of operation time. One's complement was therefore devised, based on inverting bits. That still did not satisfy engineers, and two's complement became the most popular representation. We will demonstrate it with an example. I will skip why it is the most efficient method and its mathematical subtleties, particularly since web developers rarely deal with it directly. Instead, let's focus on how to convert a number in practice.

Without the mathematical machinery, it is fairly simple. Take (-45)(10) and convert it into binary. What is the easiest way?

  1. Convert the positive version of the number to binary: 45(10) becomes 00101101(2).
  2. Set the most significant bit of the most significant byte to 1, giving 10101101(2). This is 173(10). Subtracting 45(10) gives 128(10), the weight of the highest bit. That is no coincidence.
  3. Invert every remaining bit. This turns 10101101(2) into 11010010(2).
  4. Finally, add 1, giving 11010011(2).

And voilà: -45 in binary. To verify it, we can calculate in the other direction using our familiar decimal conversion. There is just one thing to remember: the leading 1 indicates a negative number, so its weight is not 27 but (-2)7.

11010011(2) = 1 * (-2)7 + 1 * 26 + 0 * 25 + 1 * 24 + 0 * 23 + 0 * 22 + 1 * 21 + 1 * 20 = -128 + 64 + 16 + 2 + 1 = -128 + 83 = -45(10)

This also explains why a signed one-byte variable holds values from -128 to 127, apparently giving the negative side a larger range. "Apparently" matters here: the intervals are -128 through -1 and 0 through 127, so both have the same size. In binary, this looks as follows:

(-128)(10) = 10000000(2)
(-127)(10) = 10000001(2)
(-1)(10) = 11111111(2)
0(10) = 00000000(2)
127(10) = 01111111(2)

The logic may seem convoluted, but calculate it calmly and you will see that the most negative values really do contain almost all zeros in binary. If practical examples are not enough and you want the mathematical foundations, you can find explanations online in material such as this presentation.

We also need a way to store floating-point numbers, including fractions, in binary. Here, I admit, things get very complicated. Forget storing the values before and after the decimal point in separate bytes. Instead, meet the exponent, mantissa, scientific notation, also called exponential notation, familiar from school, and the IEEE 754 standard, which specifies how calculations should work. I will skip the exact conversion in either direction and instead recommend this article on Samouczek Programisty. It gives a practical explanation, including why 0.2 sometimes turns out to be 0.20000000298023224.

Is this used in practice?

That is a fair question. I said IT specialists should know binary basics, but given today's programming technologies, is this more an unwanted compulsory subject than a useful practical skill? Honestly, when developing web applications, you will not encounter binary numbers for 99% of your time. The same applies to most mobile and even desktop applications. Nevertheless, some situations make this knowledge very useful.

The first is embedded systems and other programs written at a low level. I believe people who work daily with C code also encounter bitwise operations from time to time, because they often provide an optimization opportunity. People programming a Raspberry Pi, even in Python, should also expect to need this knowledge, let alone those programming microcontrollers and other systems closely tied to hardware.

Sometimes we need to parse particular files, including binary ones. That means counting bytes, interpreting values, and converting them into decimal numbers or text characters. Knowledge of the encoding may be necessary too, since the most significant byte, rather than bit, may sometimes be on the right instead of the left. Depending on the person, these tasks can be delightful or intensely irritating. They do arise, however, and computing fundamentals then become very useful.

Even web applications sometimes use bitwise operations, for example when representing flags controlling an operation. PHP has several such functions, including json_decode. In some projects, we have also stored and transmitted permission information as bits. We could use a more readable approach, but sometimes performance and keeping that information somewhat hidden from the user matter too. Each case needs to be considered in context.

Finally, if you have ever divided a network into subnets, you may have encountered binary, particularly with a prefix length that is not a multiple of eight, such as 16 or 24. Determining address ranges then involves writing the address in binary. Have you ever wondered why IPv4 addresses range from 0.0.0.0 to 255.255.255.255 rather than, say, 999.999.999.999? You can now probably see that each of the four components is a separate byte, with a maximum value of 255. MAC addresses and IPv6 addresses contain only digits and letters from A to F for a reason too: they are written in hexadecimal.

Conclusion

I realize that working through binary may not be particularly exciting for many of you. Still, I hope people learning computing, preparing for a test on this subject, including IT vocational school students, or simply enjoying mathematics find something interesting here. Hopefully, it helps them understand the subject and explore it further. Sometimes it is worth knowing the foundations of the electronic machines we use every day and encounter everywhere.

Best regards, and thank you. Jakub Rojek.