Download Bits And Byts, Lecture Slide - Computer Science and more Slides Introduction to Computers in PDF only on Docsity!
Bits and Bytes
CS 213
Aug. 27, 1998
Topics
- Why bits?
- Representing information as bits
- Binary/Hexadecimal
- Byte representations » numbers » characters and strings » Instructions
- Bit-level manipulations
- Boolean algebra
- Expressing in C
class02.ppt CS 213 F’
Why Don’t Computers Use Base 10?
Base 10 Number Representation
- That’s why fingers are known as “digits”
- Natural representation for financial transactions
- Floating point number cannot exactly represent $1.
- Even carries through in scientific notation
Implementing Electronically
- Hard to store
- ENIAC (First electronic computer) used 10 vacuum tubes / digit
- Hard to transmit
- Need high precision to encode 10 signal levels on single wire
- Messy to implement digital logic functions
- Addition, multiplication, etc.
Anatomy of an SRAM Cell
(6 transistors)
b (^) b’
bit line bit line
word line
Inverter : High input --> Low Output Low input --> High Output
SRAM Cell Principle
Inverter Amplifies
- Negative gain
- Slope < –1 in middle
- Saturates at ends
Inverter Pair Amplifies
- Positive gain
- Slope > 1 in middle
- Saturates at ends V V
Vin
0 0.2 0.4 0.6 0.8 1
0
1
Vin V
V
Slope < –
Slope > –
Byte-Oriented Memory Organization
Programs Refer to Virtual Addresses
- Conceptually very large array of bytes
- Actually implemented with hierarchy of different memory types
- SRAM, DRAM, disk
- Only allocate for regions actually used by program
- In Unix and Windows NT, address space private to particular “process” - Program being executed - Program can clobber its own data, but not that of others
Compiler + Run-Time System Control Allocation
- Where different program objects should be stored
- Multiple mechanisms: static, stack, and heap
- In any case, all allocation within single virtual address space
Encoding Byte Values
Byte = 8 bits
- Binary 000000002 to (^111111112)
- Decimal: 010 to (^25510)
- Hexadecimal 0016 to FF 16
- Base 16 number representation
- Use characters ‘0’ to ‘9’ and ‘A’ to ‘F’
- Write FA1D37B 16 in C as 0xFA1D37B » Or 0xfa1d37b
A 10 1010
B 11 1011
C 12 1100
D 13 1101
E 14 1110
F 15 1111
HexDecimalBinary
Word-Oriented Memory Organization
Addresses Specify Byte
Locations
- Address of first byte in word
- Addresses of successive words differ by 4 (Sun) or 8 (Alpha)
Sun Words
Bytes Addr.
Alpha Words
Addr = 0000
Addr = 0008
Addr = 0000
Addr = 0004
Addr = 0008
Addr = 0012
Data Representations
Sizes of C Objects (in Bytes)
C Data Type Alpha Sun, PC, Mac, etc. int 4 4 long int 8 4 char 1 1 short 2 2 float 4 4 double 8 8 char * 8 4 » Or any other pointer
Byte Ordering
- Alphas, PC’s are “Little Endian” machines
- Least significant byte has lowest address
- Sun’s, Mac’s are “Big Endian” machines
- Least significant byte has highest address
show_bytes Execution Example
int a = 15213; printf("int a = 15213;\n"); show_bytes((pointer) &a, sizeof(int));
Result:
int a = 15213; 0x11ffffcb8 0x6d 0x11ffffcb9 0x3b 0x11ffffcba 0x 0x11ffffcbb 0x
Representing Integers
int A = 15213;
int B = -15213;
long int C = 15213;
Decimal: 15213 Binary: 0011 1011 0110 1101 Hex: 3 B 6 D
6D
3B
Alpha A
3B
6D
Sun A
C
FF
FF
Alpha B
C
FF
FF
Sun B
6D
3B
Alpha C
3B
6D
Sun C
Two’s complement representation (Covered next lecture)
Representing Floats
Float F = 15213.0;
IIEEE Single Precision Floating Point Representation Hex: 4 6 6 D B 4 0 0 Binary: 0100 0110 0110 1101 1011 0100 0000 0000 15213: 1110 1101 1011 01
Not same as integer representation, but consistent across machines
B
6D
Alpha F
B
6D
Sun F
char S[6] = "15213";
Representing Strings
Strings in C
- Represented by array of characters
- Each character encoded in ASCII format
- Standard 7-bit encoding of character set
- Other encodings exist, but uncommon
- Character “0” has code 0x » Digit i has code 0x30+ i
- String should be null-terminated
Compatibility
- Byte ordering not an issue
- Data are single byte quantities
- Text files generally platform independent
- Except for different conventions of line termination character!
Alpha S Sun S
Representing Instructions
int sum(int x, int y)
{
return x+y;
}
Different machines use totally different instructions and encodings
Alpha sum
FA
6B
E
C
Sun sum
- For this example, both use two 4-byte instructions
- Use differing numbers of instructions in other cases
Boolean Algebra
Developed by George Boole in 19th Century
- Algebraic representation of logic
- Encode “True” as 1 and “False” as 0
And
- A&B = 1 when both A=1 and B=
Not
Or
- A|B = 1 when either A=1 or B=
^ 0 1
Exclusive-Or (Xor)
- A^B = 1 when either A=1 or B=1, but not both