Welcome to this article where we will dive into the world of binary numbers. We will explore their conversions, arithmetic operations, and bitwise operations. Binary numbers are the language that every machine understands.

Every digital data/media/system can be dumbed down to binary! Binary numbers have significance in cryptography, low-level programming, and encoding/decoding. They are the basis of almost all the digital systems today. Let’s start this journey and expand our knowledge together.

## Understanding the Fundamentals of Binary Numbers

Binary numbers, represented using the base-2 numeral system, are a fundamental concept in digital systems and computer science. The base-2 numeral system uses only two digits to represent any number which is 0 and 1.

In contrast, the decimal numeral system, which we are most familiar with is a base-10 system that uses ten digits (0-9). Learn more about number bases here. Some pointers that you should keep in mind while working with binary numbers:

- Each digit in a binary number is called a bit.
- The rightmost bit represents the value 2^0(=1), the next bit to the left represents 2^1(=2), the next bit represents 2^2(=4), and so on.
- Each bit’s value is obtained by rising 2 to the power of its position from the right and multiplying it by the corresponding bit value (0 or 1).

### Manual Conversion of Binary to Decimal

Let’s consider a smaller binary number with 4 bits, `1010`

. To convert this binary number to decimal, we can follow these steps:

- To convert a binary number to decimal, assign each digit a weight based on its position from
**right to left**. The rightmost digit has a weight of 2^0, the next digit has a weight of 2^1, then 2^2, and so on - Multiply each digit by its corresponding weight which means let’s go from right to left, we have 0 at the extreme right of the binary number so we will multiply 0 by 2^0, after that, we have 1 and then we will multiply will be 2^1 and so on.
- Sum up the results: 1 * 2^3 + 0 * 2^2 + 1 * 2^1 + 0 * 2^0 = 8 + 0 + 2 + 0 = 10.
- Therefore, the binary number 1010 is equal to the decimal number 10.

## Python Approach: Decimal to Binary Conversion

To convert any decimal number to binary you can use `bin()`

in python that takes an integer number as an argument and rolls out its binary form.

decimal_number = 10binary_number = bin(decimal_number)print(binary_number)

**Output:**

“0b” is a prefix written to indicate that the following number is a binary number. It serves as a visual indicator to differentiate binary numbers from other numerical representations.

## Python Approach: Binary to Decimal Conversion

You can convert a binary number to an integer using the `int(binary_number,2)`

function by specifying a base of 2. Base 2 corresponds to the binary numbers. Other bases are also present like 10 for decimal and 16 for hexadecimal, you can use these to convert the respective numerical representation to integers by just modifying the parameter of the int() function.

**Example:**

binary_number = '1010'decimal_number = int(binary_number, 2)print(decimal_number)

**Output:**

## Performing Arithmetic Operations on Binary Numbers in Python

We can perform basic arithmetic operations on binary numbers just like decimal numbers. However direct arithmetic operation is not possible. Please go through the example below:

# Binary additionbinary_num1 = '1010'binary_num2 = '1101'result = bin(int(binary_num1, 2) + int(binary_num2, 2))print(result)

**Output:**

**Explanation:**

To add the binary numbers ‘1010’ and ‘1101’ we will first convert them into decimal form using int(). Here ‘1010’ and ‘1101’ will get converted into 10 (i.e. = 1 * 2^3 + 0 * 2^2 + 1 * 2^1 + 0 * 2^0) and 13 (i.e. = 1 * 2^3 + 1 * 2^2 + 0 * 2^1 + 1 * 2^0) and after that, we get the final sum 23 represented as 10111.

Similar rules apply to other operations like subtraction, multiplication, and division. Take a glance at the examples below, we are just changing the arithmetic operations but the process to do binary arithmetic is the same:

# Binary subtractionbinary_num1 = '1101'binary_num2 = '1010'result = bin(int(binary_num1, 2) - int(binary_num2, 2))print(result)

**Output:**

# Binary multiplicationbinary_num1 = '1010'binary_num2 = '1101'result = bin(int(binary_num1, 2) * int(binary_num2, 2))print(result)

**Output:**

# Binary divisionbinary_num1 = '1101'binary_num2 = '101'result = bin(int(binary_num1, 2) // int(binary_num2, 2))print(result)

**Output:**

## Exploring Bitwise Operations in Python

In Python, bitwise operations allow you to manipulate individual bits within integers. They are highly used to design circuits and systems. These operations work by performing operations on the binary representations of the numbers. Here are the commonly used bitwise operators in Python:

### Bitwise AND

Bitwise AND (`&`

) Performs a bitwise AND operation between the corresponding bits of two numbers. The result has 1s only in the positions where both numbers have 1s. For any other combination (01, 00, 11)we will get a 0.

**Example:**

# Bitwise ANDnum1 = 0b1010num2 = 0b1100result = num1 & num2print(bin(result))

**Output:**

The code will perform a bitwise AND operation on the binary numbers `0b1010`

and `0b1100`

. The resulting binary number is `0b1000`

, which represents the decimal number 8.

The binary operations are not limited to only two numbers; you can perform the operations on more than two numbers at the same time in one line. This is true for all the operators except the NOT operation.

### Bitwise OR

The bitwise OR operator (`|`

) in Python evaluates each bit position and returns a result where each bit is set to 1 if at least one of the corresponding bits in the operands is 1. Possible combinations are 11, 10, and 01. The OR operation returns 0 only if we have a 00.

**Example:**

# Bitwise ORnum1 = 0b1010num2 = 0b1100result = num1 | num2print(bin(result))

The code will perform a bitwise OR operation on binary numbers `0b1010`

and `0b1100`

. The result is `0b1110`

, which represents the binary number 1110.

**Output:**

### Bitwise XOR

Bitwise XOR (`^`

) performs a bitwise XOR (exclusive OR) operation between the corresponding bits of two numbers. The result has 1s in the positions where the bits differ between the two numbers. In a simpler language, if the corresponding bits are same (11 or 00) the result is 0 otherwise the result is 1. If you have interest in problem solving some observations on XOR operation can be really helpful. Let’s go through the example to understand the python implementation:

# Bitwise XORnum1 = 0b110num2 = 0b111result = num1 ^ num2print(bin(result))

The code will perform a bitwise XOR operation on binary numbers `0b110`

and `0b111`

. The result is `0b110`

, which represents the binary number 110.

**Output:**

### Bitwise NOT

Bitwise NOT (`~`

) performs a bitwise NOT operation on a **single number**, inverting all the bits. This operator flips all the 1s to 0s and vice versa.

# Bitwise NOTnum = 0b1010result = ~numprint(bin(result))

The code applies bitwise NOT (~) to binary 0b1010, resulting in 0b-1011 (two’s complement). It represents -5 in decimal form by flipping the bits and adding 1.

**Output:**

## Wrapping Up: Binary Numbers and Their Operations in Python

In this article, we’ve delved into the fascinating world of binary numbers and their operations in Python. From understanding the basics to performing complex bitwise operations, we’ve seen how Python simplifies working with binary numbers. As we continue to explore Python’s capabilities, what other intriguing aspects of this language are you excited to uncover?

**Below are a few interesting AskPython articles you can go through**:

- 4 ways to Convert a Binary String to a Normal String.
- Python Bit Manipulation and Masking Techniques.