Bits and Bytes Topics Topics Why bits? Tell me Representing information as bits Binary/Hexadecimal Byte representations » numbers » characters and strings » Instructions Bit-level manipulations – READ BOOK Boolean algebra Expressing in C CS 105 bits.ppt CS 105 “Tour of the Black Holes of Computing!”
22
Embed
Bits and Bytes Topics Why bits? Tell me Representing information as bits Binary/Hexadecimal Byte representations »numbers »characters and strings »Instructions.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Bits and BytesBits and BytesTopicsTopics
Why bits? Tell me Representing information as bits
Binary/HexadecimalByte representations
» numbers» characters and strings» Instructions
Bit-level manipulations – READ BOOKBoolean algebraExpressing in C
CS 105bits.ppt
CS 105“Tour of the Black Holes of Computing!”
– 2 – CS 105
Binary RepresentationsBinary RepresentationsBase 2 Number RepresentationBase 2 Number Representation
Represent 1521310 as 111011011011012
Represent 1.2010 as 1.0011001100110011[0011]…2
Represent 1.5213 X 104 as 1.11011011011012 X 213
Electronic ImplementationElectronic Implementation Easy to store with bistable elements - relays Reliably transmitted on noisy and inaccurate wires (media)
Straightforward implementation of arithmetic functions
0.0V
0.5V
2.8V
3.3V
0 1 0
– 3 – CS 105
Byte-Oriented Mem OrganizationByte-Oriented Mem Organization
Programs Refer to Virtual AddressesPrograms Refer to Virtual Addresses Conceptually very large array of bytes Actually implemented with hierarchy of different memory
types – SRAM, DRAM, Disk System provides 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 AllocationCompiler + Run-Time System Control Allocation Where different program objects should be stored Multiple mechanisms: static, stack, heap, etc. All allocation within single virtual address space
show_bytes Execution Exampleshow_bytes Execution 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 0x00
0x11ffffcbb 0x00
– 9 – CS 105
Representing IntegersRepresenting Integers
int A = 15213;int A = 15213;int B = -15213;int B = -15213;long int C = 15213;long int C = 15213;
Decimal: 15213
Binary: 0011 1011 0110 1101
Hex: 3 B 6 D
6D3B0000
IA32, x86-64 A
3B6D
0000
Sun A
93C4FFFF
IA32, x86-64 B
C493
FFFF
Sun B
Two’s complement representation(Covered later)
00000000
6D3B0000
x86-64 C
3B6D
0000
Sun C
6D3B0000
IA32 C
– 10 – CS 105
Byte OrderingByte Ordering
How should bytes within multi-byte word be ordered in How should bytes within multi-byte word be ordered in memory? memory? Religious IssueReligious Issue
ConventionsConventions Suns, PowerPCs are “Big Endian” machines
Least significant data byte has highest addressMost significant data byte has lowest address
Alphas, PCs are “Little Endian” machinesLeast significant byte has lowest address
– 11 – CS 105
Byte Ordering ExampleByte Ordering Example
Big EndianBig Endian Least significant byte has highest address
Little EndianLittle Endian Least significant byte has lowest address
ExampleExample Variable x has 4-byte representation 0x01234567 Address given by &x is 0x100
Deciphering NumbersDeciphering Numbers Value: 0x12ab Pad to 4 bytes: 0x000012ab Split into bytes: 00 00 12 ab Reverse: ab 12 00 00
– 13 – CS 105
Representing PointersRepresenting Pointersint B = -15213;int B = -15213;int *P = &B; // p is pointer, address values are int *P = &B; // p is pointer, address values are
different on different machines.different on different machines.
FF7F0000
0C89ECFF
x86-64 P
Different compilers & machines assign different locations to objects
FB2C
EFFF
Sun P
FFBF
D4F8
IA32 P
– 14 – CS 105
Machine-Level Code RepresentationMachine-Level Code RepresentationEncode Program as Sequence of InstructionsEncode Program as Sequence of Instructions
Each simple operationArithmetic operationRead or write memoryConditional branch
Instructions encoded as bytesAlpha’s, Sun’s, Mac’s use 4 byte instructions
» Reduced Instruction Set Computer (RISC)PC’s use variable length instructions
» Complex Instruction Set Computer (CISC)
Different instruction types and encodings for different machines
Most code not binary compatible
Programs are Byte Sequences Too - von Neumman!Programs are Byte Sequences Too - von Neumman!
With extra property that every value is its own additive inverse
A ^ A = 0
BABegin
BA^B1
(A^B)^B = AA^B2
A(A^B)^A = B3
ABEnd
*y*x
– 21 – CS 105
Main PointsMain Points
It’s All About Bits & BytesIt’s All About Bits & Bytes Numbers Programs Text
Different Machines Follow Different ConventionsDifferent Machines Follow Different Conventions Word size Byte ordering Representations
Boolean Algebra is Mathematical BasisBoolean Algebra is Mathematical Basis Basic form encodes “false” as 0, “true” as 1 General form like bit-level operations in C