Binary Operations and Sign

Bit Shifts (Multiplication and Division by 2^n)

A. Left Shift

Left Shift: Shifting left n-bits multiplies the number by 2^n

Example: Left shift one bit 0001 << 1 = 0010

B. Logical Shift

Logical Shift: Shifting right n-bits divides the unsigned number by 2^n

Example: Logical shift one bit 1000 >>> 1 = 0100

C. Arithmetic Shift

Arithmetic Shift: Shifting right n-bits divides the signed number by 2^n

Example: Arithmetic shift one bit 1000 >> 1 = 1100

Sign Extension

Introduction

Q: In the decimal system, we can denote sign by using the + and - symbol (e.g., +5, -5). How can we do the same for binary?

A: Let the MSBit (aka: signed bit) denote whether the number is positive or negative.

There are two ways to denote sign extension in binary:

A. One’s Complement

One’s Complement:

How-to Make a Value Negative in One’s Complement:

Example: Creating a negative number in one’s complement 0101_2 = +5_{10} \\ \text{(Flip every bit)} \\ 1010_2 = -5_{10}

Issues with Ones’s Complement:

  1. Two Representations of 0
  1. Carry Bit

Example: Carry Bit (adding one’s complement integers)

\begin{aligned} &0111 \\ +&1100 \end{aligned}

\begin{aligned} &0111 \\ +&1100 \\ =&0011 \\ \end{aligned}

\begin{aligned} &0011 +1 \\ &=0100 \end{aligned}

B. Two’s Complement

Two’s Complement:

How-to Make a Value Negative in Two’s Complement:

Example: Creating a negative number in two’s complement 0101_2 = +5_{10} \\ \text{(Flip every bit, +1)} \\ 1011_2 = -5_{10}

Issues with Two’s Complement: We have one more negative value than positive values.

Shortcut for Finding 2’s Complement: \text{Given: } 11100110

  1. We can tell from the MSBit that this is a negative number.
  2. The shortcut: Flip all bits left of the rightmost 1 bit. (\textcolor{green}{111001})'10 \\ \textcolor{green}{000110}10
  3. Now, to find the magnitude we just convert 11010_2 to decimal. 11010_2 = 26_{10}
  4. Therefore, the signed 8-bit 2’s complement binary string of 11100110 is equal to -26 in decimal.

Note: Do this in reverse to convert a binary number to 2’s complement.

Sign Extension:

Bit Masking (Odd/Even)

Odd or Even: Mask LSBit

How-to Mask the LSBit
Q: How do we turn 1011 into the LSBit (0001)?

A: Use boolean logic (\land)! 1011 \land 0001 = 1

Binary Addition (2’s Complement)

Note: The binary numbers are signed 2’s complement

\text{Problem: } 5 - 10

Binary Solution: \begin{aligned} &0000 0101 \text{ (5)} \\ + &1111 0110 \text{ (-10)} \\ = &1111 1011 \text{ (-5)} \end{aligned}

Binary Subtraction

Subtraction is just addition two’s complement.

Tangent: Keeping Track of Time

In UNIX, time is tracked as the number of milliseconds since 00:00:00 Jan 1, 1970 GMT (Greenwich Mean Time) / UTC (Universal Time Code).

UNIX File Dates

  1. Creation Date
  2. Access Date
  3. Modify Date

DOS File Dates

Total: 16 Bits

Note: NTFS file system now stores dates as a 32-bit/64-bit timestamp.

Example: Masking for various parts of DOS date format \begin{aligned} \text{DOS File Date} &\land 1F = \text{Day of Month} \\ \text{DOS File Date Shifted Right 5 Bits} &\land 1EO = \text{Day of Month} \end{aligned}