# Arithmetic and Logical Operations

Source: https://wisconsin.twango.dev/sp25-cs252/reference/arithmetic-and-logical-operations

# Arithmetic and Logical Operations

## Data

Represents fundamental information such as numbers, characters, and other values.

Data are particular values that conform to specific types and structures

> [!example] Examples of Simple Value Data (Primitives)
> - Integers
> - Floating-point numbers
> - Characters
> - Booleans

## Transmission

Data is transmitted between components of a computer system electronically

### Voltage

Electric potential difference: energy per unit charge.

### Current

The rate of electric charge flow.

## Binary

Most computers today are binary digital machines that electronically process data in the form of binary digits (bits)

### Digital vs. Analog

Digital has a finite number of values, while analog has an infinite number of values.

#### Binary

Binary is a base-2 number system that uses two digits, 0 and 1. Therefore, it can be represented in 2 states.

> [!example] Example
> A low state can be represented as a voltage range of 0-0.8V, while a high state can be represented as a voltage range of 2-5V.

### Bit

The basic unit of information in computing and digital communications, representing a single binary digit.

#### Grouping Bits

Bits are grouped into larger units to represent more complex data.

- Byte: 8 bits (representing 256 values)
- 10 bits: 1,024 values
- 12 bits: 4,096 values
- $n$ bits: $2^n$ values

> [!tip] Tip
> To calculate the number of bits needed to represent $n$ values, use $b=\lceil\log_2 n\rceil$ bits, for $n\ge1$

### Encoding

The process of converting one representation of data into another representation.

#### Weighted Positional Notation

Given a base $b$ and a set of $n$ digits, the value of a number is calculated as:

$$
\sum_{i=0}^{n-1} d_i \cdot b^i
$$

##### Base Systems

- Base-2: Binary
- Base-8: Octal
- Base-10: Decimal (most common)
- Base-16: Hexadecimal
- Base-64: Used in encoding

##### Most Significant Digit (MSD)

The digit that represents the largest value in a number.

##### Least Significant Digit (LSD)

The digit that represents the smallest value in a number.

### Unsigned Integers

Unsigned integers are nonnegative integers that are represented in binary form.

### Signed Integers

A data type to represent both positive and negative numbers.

#### Sign-Magnitude

The most significant bit (MSB) represents the sign of the number.

- 0: Positive
- 1: Negative

However, this method has two representations for zero.

#### One's Complement

The negative of a number is represented by flipping all the bits.

However, this method has two representations for zero.

#### Two's Complement

The negative of a number is represented by flipping all the bits and adding 1.

This method has only one representation for zero.

> [!question] What is the 2's complement of the following (taking the 2's complement)
> - $01000_2 \rightarrow 10111_2 + 1 = 11000_2$
> - $10100_2 \rightarrow 01011_2 + 1 = 01100_2$
> - $00011_2 \rightarrow 11100_2 + 1 = 11101_2$
> - $11111_2 \rightarrow 00000_2 + 1 = 00001_2$

Two's complement is the most common method for representing signed integers, as it is preferred for arithmetic operations.

Related: [[lectures/lecture-02]], [[lectures/lecture-03]], [[lectures/lecture-04]], [[lectures/lecture-05]]

## Source

Original retained in wisconsin-legacy.
