Skip to content

Binary

Binary is a base-2 number system that uses only two digits: 0 and 1, and are used by computers to store and process data. In the binary system, each digit, known as a "bit" (short for binary digit), can only be either 0 or 1.

These binary digits are combined to represent numerical values and form the basis for encoding complex information within computers. Understanding how to convert between decimal (base-10) and binary is crucial for working with low-level data and programming.

Conversion from Decimal

Here's some math explaining the rough idea behind converting a decimal number to binary:

Let n=the decimal number to convertLet binary=empty stringWhile n>0binary=(nmod2)+binaryn=n/2Return binary

Most languages do have inbuilt functions to convert between binary and decimal, but if you were to do implement it yourself, here's how you would do it:

For the bored

python
def decimal_to_binary(decimal: int) -> str:
	binary = ""
	while decimal > 0:
		binary = str(decimal % 2) + binary
		decimal = decimal // 2
	return binary	

print(decimal_to_binary(10)) # 1010
js
function decimalToBinary(decimal) {
	let binary = "";
	while (decimal > 0) {
		binary = (decimal % 2) + binary;
		decimal = Math.floor(decimal / 2);
	}
	return binary;
}

console.log(decimalToBinary(10)); // 1010
cpp
#include <iostream>
#include <string>
using namespace std;

string decimalToBinary(int decimal) {
	string binary = "";
	while (decimal > 0) {
		binary = to_string(decimal % 2) + binary;
		decimal /= 2;
	}
	return binary;
}

int main() {
	cout << decimalToBinary(10) << endl; // 1010
}
rust
fn decimal_to_binary(decimal: i32) -> String {
	let mut binary = String::new();
	let mut decimal = decimal;
	while decimal > 0 {
		binary = format!("{}{}", decimal % 2, binary);
		decimal /= 2;
	}
	binary
}

fn main() {
	println!("{}", decimal_to_binary(10)); // 1010
}

For the not-so-bored

You can, of course, use the inbuilt functions to convert between binary and decimal if you value your time:

python
print(bin(10)) # 0b1010
js
console.log((10).toString(2)); // 1010
cpp
#include <iostream>
#include <bitset>
using namespace std;

int main() {
	cout << bitset<4>(10) << endl; // 1010
}
rust
fn main() {
	println!("{:b}", 10); // 1010
}

Conversion from Octal

Here's a quick explanation of how you would convert from octal to binary:

Let's say we had an octal number (onon1o2o1)8, where oi represents the i-th digit of the octal number. To convert this octal number to binary, we can use the following steps:

  1. First, we need to convert each octal digit to its equivalent 3-bit binary representation:
o1:Convert o1 to binaryo2:Convert o2 to binaryon1:Convert on1 to binaryon:Convert on to binary
  1. Concatenate the binary representations obtained in step 1 to form the binary equivalent of the octal number:
(onon1o2o1)8=(bnbn1b2b1)2

Here's an example with specific values:

Let's convert the octal number (352)8 to binary:

o1=2b1=010o2=5b2=101o3=3b3=011

Now concatenate the binary representations:

(352)8=(011101010)2

So, (352)8 in octal is equivalent to (011101010)2 in binary.


Luckily, most languages have inbuilt functions to convert between octal and binary, so you would have no need to implement it yourself:
python
def octal_to_binary(octal: str) -> str:
	return bin(int(octal, 8))[2:]

print(octal_to_binary("12")) # 1010
js
function octalToBinary(octal) {
	return parseInt(octal, 8).toString(2);
}

console.log(octalToBinary("12")); // 1010
cpp
#include <iostream>
#include <bitset>
using namespace std;

int main() {
	cout << bitset<4>(stoi("12", 0, 8)) << endl; // 1010
}
rust
fn main() {
	println!("{:b}", i32::from_str_radix("12", 8).unwrap()); // 1010
}

Conversion from Hexadecimal

Similarly, you can convert from hexadecimal to binary (duh) using the following steps:

Let's say you have a hexadecimal number X16 with digits xnxn1x2x1x0. To convert this hexadecimal number to binary, you can use the following steps:

B2=bnbn1b2b1b0Binary representation

Refer to the Conversion Table page for the hexadecimal to binary conversion table.

So let's say you wanted to convert 420F to binary:

4:01002:00100:0000F:1111

So, 420F in hexadecimal is equivalent to 0100001000001111 in binary.

Likewise, most languages have inbuilt functions to convert between hexadecimal and binary:

python
def hex_to_binary(hexadecimal: str) -> str:
	return bin(int(hexadecimal, 16))[2:]

print(hex_to_binary("420F"))
js
function hexToBinary(hexadecimal) {
	return parseInt(hexadecimal, 16).toString(2);
}

console.log(hexToBinary("420F"));
cpp
#include <iostream>
#include <bitset>
using namespace std;

int main() {
	cout << bitset<16>(stoi("420F", 0, 16)) << endl;
}
rust
fn main() {
	println!("{:b}", i32::from_str_radix("420F", 16).unwrap());
}