Number Representations

https://slides.com/georgelee/ics141-data-representation/live

Integer Notations

Base 10 Expansions

People naturally think of numbers in base 10. What does it mean to think of a number in base 10?

 

Each digit represents a power of 10. For example:

 

141 = 1 * 10^2 + 4 * 10^1 + 1 * 10^0

Base 2 Expansions

We know computers work in binary (on/off). What does a base 2 expansion of a binary number look like? What is the base 2 expansion of (1000 1101)2? What is the value of this number?

Octal and Hexadecimal

Octal Notation

Looking at binary is kinda rough. We can group these bits to make them easier to read. We can group bits into groups of 3. Since 2^3 = 8, we have octal.

 

Our previous example was (1000 1101)2. In octal, it would be (010 001 101)2 = (215)8.

 

What is the decimal value of (117)8?

Example: Unix Permissions

Users may have 3 types of permissions (or any combination): Read, write, execute. There's also separate permissions for owner, group, and everyone.

 

Since the permission is on/off, we can represent this in octal notation.

 

What permissions would 777 have? 755? 644? 400?

 

Hexadecimal Notation

We can also use groups of 4 to represent the bits. This is hexadecimal notation (2^4 = 16). This is the most common representation of bits.

 

The values are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. So our example would be (1000 1101)2 = (8D)16.

 

What's the decimal value of (6F)16?

Converting from Decimal

Base Conversion

Recall the definition of a base 10 expansion. We can generate an expansion by successively dividing by 10. The remainder at each step is the digit in the number going from right to left.

Example: Base 10

Consider 311.

 

* 311 / 10 = 31 * 10 + 1

* 31 / 10 = 3 * 10 + 1

* 3 / 10 = 0 * 10 + 3

 

Digits go from bottom to top, and we have 311.

Example: Base 8

Now let's convert 311 to base 8.

 

* 311 / 8 = 38 * 8 + 7

* 38 / 8 = 4 * 8 + 6

* 4 / 8 = 0 * 8 + 4

 

Thus, 311 = (467)8.

Example: Base 16

Your turn. Convert 241 to base 16.

 

Signed/Unsigned Integers

Signed Integers

Thus far, we have been working with positive integers. To represent a negative integer, we need something to represent the sign.

 

For a signed integer, the leftmost (most significant) bit is the sign bit. 0 is positive and 1 is negative.

 

One's Complement

The simplest way to represent signed integers is using 1's complement. Positive integers remain the same (with a leading 0). Negative integers are represented by taking the positive representation and flipping all the bits (1's become 0's and 0's become 1's).

Example:

Consider 3 = (0011)2. What is -3 using one's complement?

 

What happens when you add the two together?

Two's Complement

The more common way to represent signed integers is using 2's complement. Positive integers remain the same. For negative integers, we take the positive representation and flip all the bits. Then, we add 1.

Example:

Consider 7 = (0111)2. What is the two's complement of 7?

 

What happens when you add the two together?

Data Representations

By George Lee

Data Representations

  • 870