Computer Arithmetic

Published on 2022-05-03

Category: Misc

Computer Arithmetic is a broad branch in the fields of Computer Science and Computer Engineering. In this overview, we'll cover essential topics such as binary representation of integers, two's complement encoding, floating-point encoding, and logic gates. While there's much more to computer arithmetic, this post aims to provide a brief introduction to these key concepts.

Binary Representation of Numbers

Binary Representation of Numbers
Figure: Binary representation of integers 0–15

The chart above shows the binary representation of integers from 0 to 15. While hexadecimal values are also important, they won't be covered in this post.

To understand the math behind converting between decimal and binary, I recommend these two YouTube videos by The Organic Chemistry Tutor:

Two’s Complement Encoding

While positive integers are represented straightforwardly in binary, negative integers use two's complement encoding. This method simplifies arithmetic operations, especially addition involving negative numbers. Without two's complement, handling negative numbers would require more complex subtraction operations.

The basic formula for two's complement encoding is:

2N - x

Where:

Let's take the number -1 as an example, using 5 bits:

25 - 1 = 32 - 1 = 31

Now, convert 31 to binary in 5 bits:

11111

You can apply this method to any negative number with any number of bits using the formula above!

Floating-Point Encoding

Floating-point encoding can seem intimidating, especially when working with scientific notation. Floating-point numbers are typically represented in a 32-bit format divided into three sections:

Example 1:

Convert the binary scientific notation 1.11111 x 2111 to floating-point encoding.

Sign Bit: Since the number is positive, the sign bit is 0.

Exponent:

Fraction:

Final Floating-Point Encoding:

Sign:       0
Exponent: 10000110
Fraction: 11111000000000000000000
Floating-Point Encoding Example 1
Figure: Floating-point encoding of 1.11111 x 2111

Example 2:

Convert the binary scientific notation -1.010101 x 2-0110001 to floating-point encoding.

Sign Bit: Since the number is negative, the sign bit is 1.

Exponent:

Fraction:

Final Floating-Point Encoding:

Sign:       1
Exponent: 01001110
Fraction: 01010100000000000000000
Floating-Point Encoding Example 2
Figure: Floating-point encoding of -1.010101 x 2-0110001

Logic Gates

Logic gates are fundamental building blocks of digital circuits. They are used in transistors, which in turn build processors for CPUs. Logic gates are also essential for constructing functional units.

The four main types of logic gates are:

AND Gate

AND Gate
Figure: AND Gate Symbol and Truth Table

The AND gate outputs 1 only if both inputs are 1.

OR Gate

OR Gate
Figure: OR Gate Symbol and Truth Table

The OR gate outputs 1 if at least one input is 1.

Exclusive OR (XOR) Gate

XOR Gate
Figure: XOR Gate Symbol and Truth Table

The XOR gate outputs 1 if the inputs are different.

NOT Gate

NOT Gate
Figure: NOT Gate Symbol and Truth Table

The NOT gate inverts the input signal.

Conclusion

This brief overview covered some fundamental concepts in computer arithmetic, including binary representation, two's complement encoding, floating-point encoding, and logic gates. Understanding these basics is essential for deeper exploration into computer architecture and digital systems.