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:
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
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
function decimalToBinary(decimal) {
let binary = "";
while (decimal > 0) {
binary = (decimal % 2) + binary;
decimal = Math.floor(decimal / 2);
}
return binary;
}
console.log(decimalToBinary(10)); // 1010
#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
}
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:
print(bin(10)) # 0b1010
console.log((10).toString(2)); // 1010
#include <iostream>
#include <bitset>
using namespace std;
int main() {
cout << bitset<4>(10) << endl; // 1010
}
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
- First, we need to convert each octal digit to its equivalent 3-bit binary representation:
- Concatenate the binary representations obtained in step 1 to form the binary equivalent of the octal number:
Here's an example with specific values:
Let's convert the octal number
Now concatenate the binary representations:
So,
Luckily, most languages have inbuilt functions to convert between octal and binary, so you would have no need to implement it yourself:
def octal_to_binary(octal: str) -> str:
return bin(int(octal, 8))[2:]
print(octal_to_binary("12")) # 1010
function octalToBinary(octal) {
return parseInt(octal, 8).toString(2);
}
console.log(octalToBinary("12")); // 1010
#include <iostream>
#include <bitset>
using namespace std;
int main() {
cout << bitset<4>(stoi("12", 0, 8)) << endl; // 1010
}
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
Refer to the Conversion Table page for the hexadecimal to binary conversion table.
So let's say you wanted to convert
So,
Likewise, most languages have inbuilt functions to convert between hexadecimal and binary:
def hex_to_binary(hexadecimal: str) -> str:
return bin(int(hexadecimal, 16))[2:]
print(hex_to_binary("420F"))
function hexToBinary(hexadecimal) {
return parseInt(hexadecimal, 16).toString(2);
}
console.log(hexToBinary("420F"));
#include <iostream>
#include <bitset>
using namespace std;
int main() {
cout << bitset<16>(stoi("420F", 0, 16)) << endl;
}
fn main() {
println!("{:b}", i32::from_str_radix("420F", 16).unwrap());
}