Bits and Bytes Jan. 20, 2000Why Don’t Computers Use Base 10?Binary RepresentationsByte-Oriented Memory OrganizationEncoding Byte ValuesMachine WordsWord-Oriented Memory OrganizationData RepresentationsByte OrderingExamining Data Representationsshow_bytes Execution ExampleRepresenting IntegersRepresenting PointersRepresenting FloatsRepresenting StringsMachine-Level Code RepresentationRepresenting InstructionsBoolean AlgebraApplication of Boolean AlgebraProperties of & and | OperationsProperties of Rings & Boolean AlgebrasRing Boolean AlgebraProperties of & and ^Relations Between OperationsGeneral Boolean AlgebrasBit-Level Operations in CContrast: Logic Operations in CShift OperationsCool Stuff with XorBits and BytesJan. 20, 2000Topics•Why bits?•Representing information as bits–Binary/Hexadecimal–Byte representations»numbers»characters and strings»Instructions •Bit-level manipulations–Boolean algebra–Expressing in CCS 213 S’00class02.ppt15-213“The Class That Gives CMU Its Zip!”CS 213 S’00– 2 –class02.pptWhy 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.20•Even carries through in scientific notation–1.5213 X 104Implementing 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.CS 213 S’00– 3 –class02.pptBinary RepresentationsBase 2 Number Representation•Represent 1521310 as 111011011011012•Represent 1.2010 as 1.0011001100110011[0011]…2•Represent 1.5213 X 104 as 1.11011011011012 X 213Electronic Implementation•Easy to store with bistable elements•Reliably transmitted on noisy and inaccurate wires •Straightforward implementation of arithmetic functions0.0V0.5V2.8V3.3V0 1 0CS 213 S’00– 4 –class02.pptByte-Oriented Memory OrganizationPrograms 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 othersCompiler + 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 spaceCS 213 S’00– 5 –class02.pptEncoding Byte ValuesByte = 8 bits•Binary 000000002 to 111111112•Decimal: 010to 25510•Hexadecimal 0016 to FF16–Base 16 number representation–Use characters ‘0’ to ‘9’ and ‘A’ to ‘F’–Write FA1D37B16 in C as 0xFA1D37B»Or 0xfa1d37b0 0 00001 1 00012 2 00103 3 00114 4 01005 5 01016 6 01107 7 01118 8 10009 9 1001A 10 1010B 11 1011C 12 1100D 13 1101E 14 1110F 15 1111HexDecimalBinaryCS 213 S’00– 6 –class02.pptMachine WordsMachine Has “Word Size”•Nominal size of integer-valued data–Including addresses•Most current machines are 32 bits (4 bytes)–Limits addresses to 4GB–Becoming too small for memory-intensive applications•High-end systems are 64 bits (8 bytes)–Potentially address 1.8 X 1019 bytes•Machines support multiple data formats–Fractions or multiples of word size–Always integral number of bytesCS 213 S’00– 7 –class02.pptWord-Oriented Memory OrganizationAddresses Specify Byte Locations•Address of first byte in word•Addresses of successive words differ by 4 (32-bit) or 8 (64-bit)00000001000200030004000500060007000800090010001132-bitWordsBytes Addr.001200130014001564-bitWordsAddr =0000Addr =0008Addr =0000Addr =0004Addr =0008Addr =0012CS 213 S’00– 8 –class02.pptData RepresentationsSizes of C Objects (in Bytes)C Data Type Compaq Alpha Typical 32-bitint 4 4long int 8 4char 1 1short 2 2float 4 4double 8 8char * 8 4»Or any other pointerCS 213 S’00– 9 –class02.pptByte OrderingIssue•How should bytes within multi-byte word be ordered in memoryConventions•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 addressExample•Variable x has 4-byte representation 0x1234567•Address given by &x is 0x1000x100 0x101 0x102 0x10301 23 45 670x100 0x101 0x102 0x10367 45 23 01Big EndianLittle EndianCS 213 S’00– 10 –class02.pptExamining Data RepresentationsCode to Print Byte Representation of Data•Cast pointer to unsigned char * creates byte arraytypedef unsigned char *pointer;void show_bytes(pointer start, int len){ int i; for (i = 0; i < len; i++) printf("0x%p\t0x%.2x\n", start+i, start[i]); printf("\n");}Printf directives:%p: Print pointer%x: Print HexadecimalCS 213 S’00– 11 –class02.pptshow_bytes Execution Exampleint a = 15213;printf("int a = 15213;\n");show_bytes((pointer) &a, sizeof(int));Result:int a = 15213;0x11ffffcb8 0x6d0x11ffffcb9 0x3b0x11ffffcba 0x000x11ffffcbb 0x00CS 213 S’00– 12 –class02.pptRepresenting Integersint A = 15213;int B = -15213;long int C = 15213;Decimal: 15213Binary: 0011 1011 0110 1101Hex: 3 B 6 D6D3B0000Alpha A3B6D0000Sun A93C4FFFFAlpha BC493FFFFSun B000000006D3B0000Alpha C3B6D0000Sun CTwo’s complement representation(Covered next lecture)CS 213 S’00– 13 –class02.pptRepresenting Pointersint B = -15213;int *P = &B;Alpha AddressHex: 1 F F F F F C A 0Binary: 0001 1111 1111 1111 1111 1111 1100 1010 000001000000A0FCFFFFAlpha PFB2CEFFFSun PSun AddressHex: E F F F F B 2 C Binary: 1110 1111 1111 1111 1111 1011 0010 1100Different compilers & machines assign different locations to objectsCS 213 S’00– 14 –class02.pptRepresenting FloatsFloat F = 15213.0;IEEE Single Precision Floating Point RepresentationHex: 4 6 6 D B 4 0 0 Binary: 0100 0110 0110 1101 1011 0100 0000 000015213: 1110 1101 1011 01Not same as integer representation, but consistent across machines00B46D46Alpha FB400466DSun FCS 213 S’00– 15 –class02.pptchar S[6] = "15213";Representing StringsStrings in C•Represented by array of characters•Each character encoded in ASCII format–Standard
View Full Document