Understanding Bits, Bytes, and Number Representations in Computing - Prof. Jian-Guo Liu, Exams of Mathematics

An introduction to the concept of bits and bytes, explaining why computers use binary representations instead of base 10, and discussing the electronic implementation and memory organization of binary data. It also covers the encoding of byte values and machine words, as well as the representation of integers, pointers, floats, and strings.

Typology: Exams

Pre 2010

Uploaded on 07/30/2009

koofers-user-oy4
koofers-user-oy4 🇺🇸

10 documents

1 / 34

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Bits and Bytes
Aug. 29, 2002
Topics
Topics
nWhy bits?
nRepresenting information as bits
lBinary/Hexadecimal
lByte representations
»numbers
»characters and strings
»Instructions
nBit-level manipulations
lBoolean algebra
lExpressing in C
15-213 F’02
class02.ppt
15-213
“The Class That Gives CMU Its Zip!”
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22

Partial preview of the text

Download Understanding Bits, Bytes, and Number Representations in Computing - Prof. Jian-Guo Liu and more Exams Mathematics in PDF only on Docsity!

Bits and Bytes

Aug. 29, 2002

Topics Topics

n Why bits?

n Representing information as bits

l Binary/Hexadecimal l Byte representations » numbers » characters and strings » Instructions

n Bit-level manipulations

l Boolean algebra l Expressing in C

class02.ppt 15-213 F’

“The Class That Gives CMU Its Zip!”

Why Don’t Computers Use Base 10? Why Don’t Computers Use Base 10?

Base 10 Number Representation Base 10 Number Representation

n That’s why fingers are known as “digits”

n Natural representation for financial transactions

l Floating point number cannot exactly represent $1.

n Even carries through in scientific notation

l 1.5213 X 10^4

Implementing Electronically Implementing Electronically

n Hard to store

l ENIAC (First electronic computer) used 10 vacuum tubes / digit

n Hard to transmit

l Need high precision to encode 10 signal levels on single wire

n Messy to implement digital logic functions

l Addition, multiplication, etc.

Byte-Oriented Memory Organization Byte-Oriented Memory Organization

Programs Refer to Virtual Addresses Programs Refer to Virtual Addresses

n Conceptually very large array of bytes

n Actually implemented with hierarchy of different memory

types

l SRAM, DRAM, disk l Only allocate for regions actually used by program

n In Unix and Windows NT, address space private to particular

“process”

l Program being executed l Program can clobber its own data, but not that of others

Compiler + Run-Time System Control Allocation Compiler + Run-Time System Control Allocation

n Where different program objects should be stored

n Multiple mechanisms: static, stack, and heap

n In any case, all allocation within single virtual address space

Encoding Byte Values Encoding Byte Values

Byte = 8 bits Byte = 8 bits

n Binary 000000002 to 111111112

n Decimal: 010 to 25510

n Hexadecimal 0016 to FF 16

l Base 16 number representation l Use characters ‘0’ to ‘9’ and ‘A’ to ‘F’ l 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

Word-Oriented Memory

Organization

Addresses Specify Byte Addresses Specify Byte

Locations Locations

n Address of first byte in

word

n Addresses of successive

words differ by 4 (32-bit) or

8 (64-bit)

32-bit Words

Bytes Addr.

64-bit Words

Addr = ??

Addr = ??

Addr = ??

Addr = ??

Addr = ??

Addr = ??

0000

0004

0008

0012

0000

0008

Data Representations Data Representations

Sizes of C Objects (in Bytes) Sizes of C Objects (in Bytes)

n C Data Type Compaq Alpha Typical 32-bit Intel IA

l int 4 4 4 l long int 8 4 4 l char 1 1 1 l short 2 2 2 l float 4 4 4 l double 8 8 8 l long double 8 8 10/ l char * 8 4 4 » Or any other pointer

Byte Ordering Example Byte Ordering Example

Big Big EndianEndian

n Least significant byte has highest address

Little Little EndianEndian

n Least significant byte has lowest address

Example Example

n Variable x has 4-byte representation 0x

n Address given by &x is 0x

0x100 0x101 0x102 0x 01 23 45 67

0x100 0x101 0x102 0x 67 45 23 01

Big Endian

Little Endian

Reading Byte-Reversed Listings Reading Byte-Reversed Listings

Disassembly Disassembly

n Text representation of binary machine code

n Generated by program that reads the machine code

Example Fragment Example Fragment

Address Instruction Code Assembly Rendition 8048365: 5b pop %ebx 8048366: 81 c3 ab 12 00 00 add $0x12ab,%ebx 804836c: 83 bb 28 00 00 00 00 cmpl $0x0,0x28(%ebx)

Deciphering Numbers Deciphering Numbers

n Value: 0x12ab

n Pad to 4 bytes: 0x000012ab

n Split into bytes: 00 00 12 ab

n Reverse: ab 12 00 00

show_bytes show_bytes Execution ExampleExecution Example

int a = 15213; printf("int a = 15213;\n"); show_bytes((pointer) &a, sizeof(int));

Result (Linux):

int a = 15213; 0x11ffffcb8 0x6d 0x11ffffcb9 0x3b 0x11ffffcba 0x 0x11ffffcbb 0x

Representing Integers Representing Integers

int int AA = 15213;= 15213;

int int BB = -15213;= -15213;

long long intint C = 15213;C = 15213;

Decimal: 15213 Binary: 0011 1011 0110 1101 Hex: 3 B 6 D

6D

3B

Linux/Alpha A

3B

6D

Sun A

C

FF

FF

Linux/Alpha B

C

FF

FF

Sun B

Two’s complement representation (Covered next lecture)

6D

3B

Alpha C

3B

6D

Sun C 6D 3B 00 00

Linux C

Representing Floats Representing Floats

Float Float F = 15213.0;F = 15213.0;

IEEE 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

Linux/Alpha F

B

6D

Sun F

Can see some relation to integer representation, but not obvious

IEEE 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

IEEE Single Precision Floating Point Representation Hex: 4 6 6 D B 4 0 0 Binary: 0100 0110 0 110 1101 1011 0100 0000 0000 15213: 1110 1101 1011 01

char S[6] = "15213"; char S[6] = "15213";

Representing Strings Representing Strings

Strings in C Strings in C

n Represented by array of characters

n Each character encoded in ASCII format

l Standard 7-bit encoding of character set l Other encodings exist, but uncommon l Character “0” has code 0x » Digit i has code 0x30 + i

n String should be null-terminated

l Final character = 0

Compatibility Compatibility

n Byte ordering not an issue

l Data are single byte quantities

n Text files generally platform independent

l Except for different conventions of line termination character(s)!

Linux/Alpha S Sun S

Representing Instructions Representing Instructions

int int sum(sum(intint x,x, intint y)y)

return x+y; return x+y;

Different machines use totally different instructions and encodings

Alpha sum

FA

6B

E

C

Sun sum

n For this example, Alpha &

Sun use two 4-byte

instructions

l Use differing numbers of instructions in other cases

n PC uses 7 instructions with

lengths 1, 2, and 3 bytes

l Same for NT and for Linux l NT / Linux not fully binary compatible

E

8B

PC sum

0C

EC

5D

C

Boolean Algebra Boolean Algebra

Developed by George Developed by George BooleBoole in 19th Centuryin 19th Century

n Algebraic representation of logic

l Encode “True” as 1 and “False” as 0

And And

n A&B = 1 when both A=1 and

B=1 & 0 1

Not Not

n ~A = 1 when A=

Or Or

n A|B = 1 when either A=1 or

B=1 | 0 1

^ 0 1

Exclusive-Or ( Exclusive-Or (XorXor))

n A^B = 1 when either A=1 or

B=1, but not both