Computer Number Systems 101: Binary & Hexadecimal Conversions
Tags: beginners, codenewbie, tutorial, programming
Published: 2022-12-21 19:20:54

When we type words onto a computer, it will translate them into numbers. In fact, to a computer, all information is written as a series of 1s and 0s. **Computer number systems** are how we represent numbers in a computer system architecture.

Number systems are one of the most fundamental concepts that computer scientists must learn. It's an important step for anyone who wants to become a computer scientist or programmer. 

Today, we will introduce you to the number systems that are essential for a computer scientist. We'll take a deep dive into the binary and hexadecimal number systems.

**Today, we will learn:**
* [What is a number system in computer science?](#what-is)
* [What is the binary number system?](#binary)
* [What is the hexadecimal number system?](#hexa)
* [How to convert between binary and hexadecimal](#convert)
* [What is the octal number system?](#octal)
* [What to learn next](#next)

<a name="what-is"></a>

## What is a number system in computer science? 

Humans have been counting for a long time. To do so, we use systems that relate unique symbols to specific values. This is called a **number system**, and it is the technique that we use to represent and manipulate numbers 

> A number system must have unique symbols for every value, be consistent, provide comparable values, and be easily reproducible. 

You are probably most familiar with the **decimal system** that forms the basis of how humans count. The decimal system has a *base of 10* because it provides 10 symbols to represent all numbers:

![Computer numbers system](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l0r2yulp5cqyu7liecr8.png)

Humans use the decimal system because we have 10 fingers to count on, but machines don’t have that luxury. So, we’ve created other number systems that perform the same functions. Computers represent information differently than humans, so we need different systems to represent numbers.

**Computers support the following number systems:**
* Binary 
* Octal
* Decimal 
* Hexadecimal 

<a name="binary"></a>

## Introduction to the Binary Number System

A computer uses [**bits**](https://www.educative.io/blog/bit-manipulation-algorithm?eid=5082902844932096) to represent information. A bit is the most basic unit of storage in a computer.  An essential component of computers is called the **transistor**. Just like a light switch, the transistor either allows or prevents the current to flow. So, it only has two states: *on* and *off*. 

Every number in a computer is an electrical signal. In the early days of computers, electrical signals were represented with an “on” state (a negative charge) and an “off” state (a positive charge). This forms a kind of binary switch. 

These two states can be represented using one of two symbols: **1** and **0**. This means that the binary number system has a base of 2. Only symbols are needed to represent every number.

> The base digits for the binary system are simple: 0 to represent a low state and 1 to represent a high state.


Instead of representing numbers as individual units (like the number 10 or 400), we use groups of 1s and 0s. For example, here is what it looks like when a computer counts from 0 to 10:

![Decimals](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0i6q3mh9e35qqk1kh6k4.png)

This is called the binary number system. Each binary digit is referred to as a **bit**. For instance, *0110* is composed of 4-bits which are *0*, *1*, *1*, and *0*. When it comes to placing values to the bits (i.e. digits) in this system, we place values corresponding to ascending powers of 2 from right to left.

The rightmost bit is called the **least significant bit (LSB)**, and the leftmost bit is the **most significant bit (MSB)**. 

You can manipulate bits left and right with [**bitwise operators**](https://www.educative.io/blog/bit-manipulation-in-java?eid=5082902844932096) to efficiently change the value of a number at the machine code level.

### Convert between Decimal and Binary

Now that we know the basics of the binary system, let’s learn how we'd convert between decimal and binary. We begin with binary to decimal conversion.

We know that the binary system has place values of powers of 2. These values are weights for the digits (0 or 1) in those positions. Here's how it works:
* We multiply each digit by its weight (its position multiplied by 2)
* We sum them all up to get the decimal number

So, let's take the binary number 11111010 and convert it to the decimal number system. 

![Number System](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yb5tf875va0txyly0kba.png)

--->

![Number system 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a1os8szij7rpux7jjc8f.png)

Now let’s try it the other way around. How do we convert from a decimal to a binary number? One method to do this is repeated division, which is a handy shortcut.

So, take the number 19. We start by dividing it by two and writing down the remainder. When we divide 19 by 2, we get 9 with a remainder of 1.

We then take 9 and divide it by 2, giving us a result of 4 with a remainder of 1. This process continues until we reach zero. The remainders that we gathered form our binary number!

![Binary number](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f1krg9szbv7roezta2ww.png)

> Repeatedly dividing by 2 and keeping track of the remainders is a shortcut for converting decimal to binary!

<a name="hexa"></a>

## Introduction to the Hexadecimal Number System

Now that we've learned a bit about the binary system, let's learn about another common system used by computers: **the hexadecimal number system**.

Binary numbers are easy for computers but not so easy for humans to understand. And when you're working with large numbers, it becomes difficult to write without errors. So, to fix this problem, we can arrange binary numbers into groups of four bits, forming the hexadecimal number system.


> The hexadecimal system is a more compact way of representing numbers on a computer, as it requires only 4 bits to represent a digit's value.


The hexadecimal system (often called "hex") is made up of **16 symbols**, so it has a base of 16. Hexadecimal uses the 10 numbers of the decimal system and six extra symbols, A, B, C, D, E, and F.

![Hexadecimal system](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gt74mgd2iyb8xntyla1v.png)

The place values in hexadecimal are powers of 16. Let’s see what the hexadecimal number XYZ would be in decimal. As you can see below, hexadecimal numbers are preceded by a # to indicate that they are base-16.

![Place values](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kauqpc3c4x7ac85gi247.png)

<a name="convert"></a>

## How to convert between binary and hexadecimal

Now that we understand both the binary and the hexadecimal number systems, let’s learn how to convert between a binary number and a hexadecimal number. We begin with binary to hexadecimal.

We will group the binary digits into sets of four (starting on the right). Then, we replace each quartet with the corresponding hexadecimal representation.

![1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4285ehyci9cpku1ci0rx.png)

**--->**

![2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tzxbe3nzi44zirddet0c.png)

**--->**

![3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2yw7l6imm9qvtycf8gvd.png)

**--->**

![4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qlp7a2v081vxdk5w13u6.png)

Now for **hexadecimal to binary**! Let’s go through an example. Below, we are expanding each hexadecimal digit by replacing it with its equivalent binary quartet.

![2.1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nkh6xlbojcvf0p5ky4e0.png)

**--->**

![2.2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wsdgib1ijpv56s559rkv.png)

**--->**

![2.3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/le39mv3oh3mxs46u04dp.png)


<a name="octal"></a>

## What is the octal number system?

The octal number system is not as widely used as the hex of binary. It was developed under the same idea as the hexadecimal system: to **make binary more compact**.

The octal system groups binary numbers into **triplets** instead of quartets. So, the octal system is **base-8**, as *2^3 = 8*.

We use eight base symbols for the octal system that are borrowed from the decimal system. Binary triplets can have values ranging from *0-7*.

So, the place values will be ascending in powers of *8* from right to left.

![Octal numbers system](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xqo0xc9nwl2bmkk0seob.png)

To **convert binary to octal**, we follow this basic technique:
* Group the binary number into sets of three (similar to what we did with hex)
* Bring each group of digits to a multiple of three by adding zeroes
* Write the corresponding octal symbol underneath each group 
* You will now have an octal number

Converting **octal to binary** is similar but a bit simpler:
* Write the binary representation for each octal digit
* Join those numbers together
* You will not have a binary number

## What to learn next

Congrats! You now have a solid introduction to number systems for computer science. You've taken your first fundamental step into the world of computer programming. There is still a lot to learn, however. Your next steps are to learn:
* How to create your own number system
* Binary operations 
* Signed binary numbers
* Fixed point notation
* Basics of bits and bytes
* ASCII
* Unicode
* Basics of computer memory

Once you cover these topics, you're well on your way to understanding computers! To get started with these concepts and to explore binary, hex, and octal in greater detail, check out Educative's course **[Number Systems For Computer Scientists](https://www.educative.io/courses/number-systems-for-computer-scientists?eid=5082902844932096)**, which covers all this and more. 

You'll be introduced to the essential number systems and learn how to represent and manipulate, positive, negative, and fractional numbers in binary. No programming knowledge is needed, so get started today and jumpstart your career as a computer scientist. 

*Happy learning!*

### Continue reading about computer science on Educative
* [Absolute beginner's guide to computers and programming](https://www.educative.io/blog/beginners-guide-to-computers-and-programming?eid=5082902844932096)
* [Learn How to Code: the beginner's guide to coding and syntax](https://www.educative.io/blog/learn-how-to-code-beginners-guide?eid=5082902844932096)
* [Beginner's guide to the 5 most popular programming languages](https://www.educative.io/blog/programming-languages-guide?eid=5082902844932096)

### Start a discussion
What other introductory topics would you like to read about next? Was this article helpful? Let us know in the comments below!