# Chapter 2

## 1.2 Transistor used as a switch

Transistors operate in a so-called switching mode. ## 1.3 Capacitor as computer memory

A capacitor is an electrical device which can be used to store a charge by application of a voltage. The simplest type of capacitor consists of two parallel plates with a voltage applied across them. A capacitor is characterized by its ratio of charge stored to applied voltage. Called capacitance, measured in Farads.

Two different physical states: 1 - Charged and 0 - discharged

## 2.3 Representing integer numbers in binary format

Method:
1. Divide decimal number by 2 and record the remainder
2. Keeping dividing the quotient obtained until the new quotient is 0
3. Concatenate remainder values in reverse order to get the value

## 2.4 Binary numbers in computers

To represent significant amounts of information, bits are manipulated in groups. Always in a fixed-length format. eg, 8 bits, 16 bits, 32 bits or 64 bits. (1 byte, 2 bytes, 4 bytes and 8 bytes respectively)

Always write binary numbers in a required fixed-length format. Every bit must be defined.

Given the binary number, the leftmost big is the most significant bit. The rightmost bit is the least significant bit. The number that has the greatest impact on the size of the number (ie, the leftmost bit) is the most significant.

Converting a binary number to a hexadecimal number
1. Group every four bits in the binary number (append zeros at the MSB position if needed to make a group of 4
2. Replace each group by its corresponding hexadecimal value

Always write binary in groups of 4 bits to make it easier to convert

The prefix `\$` is used for representing a hexadecimal number.

## 2.6 Converting integers between decimal and hexadecimal

This is similar to the method for converting a decimal number to binary, expect this time divide by 16 instead of 2 and record the remainders. The remainders read backwards are the hexadecimal digits.

Note the base numbers for hexadecimal number systems are:
4096 (163)    256 (162)    16(161)    1 (160)
eg. \$3CE =     3*256      +   12*16    +   14
= 97410

## 2.7 Capacity of representation

An n-bit binary number can represent 2n different values

## 2.8 Binary arithmetic

The product of two numbers can be calculated using the shifting and adding algorithm. The multiplication of 2 4-bit numbers results in an 8-bit result. The result of (byte x byte) must be stored at least in a 2-byte unit, or accuracy will be lost.

Mask operation: using logical AND operation to force unwanted bits to zero, while leaving the other bits of interest unaffected.

Set operation: using logical OR to set chosen bits to one while leave the other bits unaffected.

## 3.2 Sign and magnitude representation

With n bits, one way of representing a signed number is to use the MSB to indicated the sign of the number. Convention is 0 for positive and 1 for negative, and the remaining bits to represent the magnitude of the number.

With n bits, the range is `-(2n-1-1) to +(2n-1-1)`

## 3.3 Complementary representation

A negative number is represented by the two's complement of the corresponding positive number. As a result, subtraction can be implemented using addition, so no subtraction (hardware or software) is required.

### 3.3.1 Obtaining the two's complement of a binary number

Method
1. Invert each bit in the number, i.e 1->0, 0->1

Method for adding two numbers with two's complement
Subtraction is performed using addition, with the following procedures:
1. Replace the negative numbers in the formula by their two's complements (using the above method)
2. Perform addition with the complements
3. Discard the carry out (if any) on the MSB side.

### 3.3.3 Interpreting numbers in two's complement arithmetic

In the two's complement domain, the base number associated with the MSB is negative - used to represent negative numbers

## 3.4 Ranges of integer numbers

 Byte (8-bit) Two bytes integer Four bytes integer unsigned signed unsigned signed unsigned signed 0 -128 0 -32768 0 -2147483648 255 +127 65535 +32768 4294967295 +2147483647

## 3.5 Overflow

Overflow happens when the result of an operation does not fir the representation being used

Positive Overflow: positive + positive = negative
Negative Overflow: (discard carry) negative + negative = postive

## 3.6 Biased representation

Signed numbers may be converted into unsigned numbers by adding a positive constant bias b.

The values are stored as positive numbers after the bias has been added. The actual signed value is retrieved by subtracting the bias from the stored values.

Biased representation is not efficient in terms of computation. It is used to encode the expenent part in IEEE floating point numbers.

## 4.2 Binary representation of real numbers

Method for finding the binary representation of a fraction
1. Multiply by 2
2. Strip the integer part from the result
3. Repeat until 0 is reached, or the desired degree of precision
4. The stripped integers, read forwards, are the fractional binary digits

Lecture 7: Real Number Arithmetic - Contains examples of this method

## 4.3 IEEE standard 754

Real numbers represented using fixed-length binary numbers, in a floating-point format.

V = ( -1 ) s x 1.f x 2e-b
s = a sign bit. 0 = positive, 1 = negative
e = exponent in excess b
f
= fractional mantissa (bit after the binary point)

The sign-and-magnitude method is used to represent the sign of a real number (decided by the sign bit `s`). The biased method is used to represent the exponent. A hidden bit is implied, which increases the representation capacity without any hardware cost.

##### Range of representation for `float` Limited length representation has limited precision. Use longer units (such as `double` or `long double`) to reduce the chances of getting into underflow or overflow, and increase the presentation precision.

NaN is typically obtained if 0 is divided by 0. Any arithmetic operations involving NaN always gives a NaN answer.

## 5.0 ASCII and Unicode

UTF-8 Uses one byte to encode English characters, and uses up to four to encode the other characters. Widely used in email systems and on the Internet

UTF-16 Uses two bytes to encode the most commonly used characters, and uses four btes to encode each of the additional characters.