bits, bytes, and integersdjp3.westmont.edu/classes/2017_01_cs045/lectures/lecture_07.pdf · bits,...

27
BITS, BYTES, AND INTEGERS CS 045 Computer Organization and Architecture Prof. Donald J. Patterson Adapted from Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

Upload: hoangque

Post on 06-Mar-2018

242 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: BITS, BYTES, AND INTEGERSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_07.pdf · BITS, BYTES, AND INTEGERS ... • Byte = 8 bits • Binary 00000000 2 to 11111111 2

BITS, BYTES, AND INTEGERS

CS 045

Computer Organization and Architecture

Prof. Donald J. PattersonAdapted from Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

Page 2: BITS, BYTES, AND INTEGERSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_07.pdf · BITS, BYTES, AND INTEGERS ... • Byte = 8 bits • Binary 00000000 2 to 11111111 2

WORK IT OUT• Create a program that will print out numbers from 1 to

100 backwards

• Create a program that

• will create an array of integers with 100 elements in it

• set each of the elements to twice its index value.

• output the address of each element of the array

• Create a function that doubles the value of its argument. Use pointers to make the changes persist even after the function call is complete.

TYPES IN C : POINTERS

Page 3: BITS, BYTES, AND INTEGERSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_07.pdf · BITS, BYTES, AND INTEGERS ... • Byte = 8 bits • Binary 00000000 2 to 11111111 2

WORK IT OUT• Create a program that will print out numbers from 1 to

100 backwards

TYPES IN C : POINTERS

Page 4: BITS, BYTES, AND INTEGERSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_07.pdf · BITS, BYTES, AND INTEGERS ... • Byte = 8 bits • Binary 00000000 2 to 11111111 2

WORK IT OUT• Create a program that will print out numbers from 1 to

100 backwards

TYPES IN C : POINTERS

Page 5: BITS, BYTES, AND INTEGERSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_07.pdf · BITS, BYTES, AND INTEGERS ... • Byte = 8 bits • Binary 00000000 2 to 11111111 2

WORK IT OUT• Create a program that

• will create an array of integers with 100 elements in it

• set each of the elements to twice its index value.

• output the address of each element of the array

TYPES IN C : POINTERS

Page 6: BITS, BYTES, AND INTEGERSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_07.pdf · BITS, BYTES, AND INTEGERS ... • Byte = 8 bits • Binary 00000000 2 to 11111111 2

WORK IT OUT• Create a program that

• will create an array of integers with 100 elements in it

• set each of the elements to twice its index value.

• output the address of each element of the array

TYPES IN C : POINTERS

Page 7: BITS, BYTES, AND INTEGERSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_07.pdf · BITS, BYTES, AND INTEGERS ... • Byte = 8 bits • Binary 00000000 2 to 11111111 2

WORK IT OUT • Create a function that doubles the value of its argument. Use pointers to make the changes persist even after the function call is complete.

TYPES IN C : POINTERS

Page 8: BITS, BYTES, AND INTEGERSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_07.pdf · BITS, BYTES, AND INTEGERS ... • Byte = 8 bits • Binary 00000000 2 to 11111111 2

WORK IT OUT • Create a function that doubles the value of its argument. Use pointers to make the changes persist even after the function call is complete.

TYPES IN C : POINTERS

Page 9: BITS, BYTES, AND INTEGERSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_07.pdf · BITS, BYTES, AND INTEGERS ... • Byte = 8 bits • Binary 00000000 2 to 11111111 2

0 1

Page 10: BITS, BYTES, AND INTEGERSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_07.pdf · BITS, BYTES, AND INTEGERS ... • Byte = 8 bits • Binary 00000000 2 to 11111111 2

BASICS• bit

• byte

• bytes that are interpreted becomes information

• instructions

• machine language

• data “types”

• unsigned integers

• signed integers (two’s-complement)

• floating point

• characters

BITS, BYTES, INTEGERS

Page 11: BITS, BYTES, AND INTEGERSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_07.pdf · BITS, BYTES, AND INTEGERS ... • Byte = 8 bits • Binary 00000000 2 to 11111111 2

BASICS• Why bits?

• Easy to store with bistable elements

• Reliably transmitted on noisy and inaccurate wires

BITS, BYTES, INTEGERS

0.0V0.2V

0.9V1.1V

0 1 0

Page 12: BITS, BYTES, AND INTEGERSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_07.pdf · BITS, BYTES, AND INTEGERS ... • Byte = 8 bits • Binary 00000000 2 to 11111111 2

BASICS• bytes are limited so computations can have errors

• overflow

BITS, BYTES, INTEGERS

Page 13: BITS, BYTES, AND INTEGERSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_07.pdf · BITS, BYTES, AND INTEGERS ... • Byte = 8 bits • Binary 00000000 2 to 11111111 2

BASICS• bytes are limited so computations can have errors

• overflow

BITS, BYTES, INTEGERS

Page 14: BITS, BYTES, AND INTEGERSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_07.pdf · BITS, BYTES, AND INTEGERS ... • Byte = 8 bits • Binary 00000000 2 to 11111111 2

BASICS• bytes are limited so computations can have errors

• underflow

BITS, BYTES, INTEGERS

Page 15: BITS, BYTES, AND INTEGERSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_07.pdf · BITS, BYTES, AND INTEGERS ... • Byte = 8 bits • Binary 00000000 2 to 11111111 2

BINARY• Despite limitations we can count in binary just like we

can in decimal

• We can represent

• 1521310 as 111011011011012

• 1.2010 as 1.0011001100110011[0011]…2

• 1.5213 X 104 as 1.11011011011012 X 213

BITS, BYTES, INTEGERS

Page 16: BITS, BYTES, AND INTEGERSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_07.pdf · BITS, BYTES, AND INTEGERS ... • Byte = 8 bits • Binary 00000000 2 to 11111111 2

BINARY• Byte = 8 bits

• Binary 000000002 to 111111112

• Decimal: 010 to 25510

• Hexadecimal 0016 to FF16

• Base 16 number representation

• Use characters ‘0’ to ‘9’ and ‘A’ to ‘F’

• Write FA1D37B16 in C as

• 0xFA1D37B

• 0xfa1d37b

BITS, BYTES, INTEGERS

0 0 0000 1 1 0001 2 2 0010 3 3 0011 4 4 0100 5 5 0101 6 6 0110 7 7 0111 8 8 1000 9 9 1001 A 10 1010 B 11 1011 C 12 1100 D 13 1101 E 14 1110 F 15 1111

HexDecimal

Binary

Page 17: BITS, BYTES, AND INTEGERSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_07.pdf · BITS, BYTES, AND INTEGERS ... • Byte = 8 bits • Binary 00000000 2 to 11111111 2

CONVERSIONS• What is 0x39A7F8 in binary

• What is 1100100101111011 in hexadecimal

• What is 0xD5E4C in binary

• What is 1001101110011110110101

WORK IT OUT

0 0 0000 1 1 0001 2 2 0010 3 3 0011 4 4 0100 5 5 0101 6 6 0110 7 7 0111 8 8 1000 9 9 1001 A 10 1010 B 11 1011 C 12 1100 D 13 1101 E 14 1110 F 15 1111

HexDecimal

Binary

Page 18: BITS, BYTES, AND INTEGERSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_07.pdf · BITS, BYTES, AND INTEGERS ... • Byte = 8 bits • Binary 00000000 2 to 11111111 2

CONVERSIONS• What are the following in hexadecimal

• 0000 00002

• 0001 00002

• 0001 00002

• 0101 01012

• 1001 10012

• 0001 00012

• 0010 00102

• 0100 01002

• 1000 10002

WORK IT OUT

0 0 0000 1 1 0001 2 2 0010 3 3 0011 4 4 0100 5 5 0101 6 6 0110 7 7 0111 8 8 1000 9 9 1001 A 10 1010 B 11 1011 C 12 1100 D 13 1101 E 14 1110 F 15 1111

HexDecimal

Binary

Page 19: BITS, BYTES, AND INTEGERSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_07.pdf · BITS, BYTES, AND INTEGERS ... • Byte = 8 bits • Binary 00000000 2 to 11111111 2

TYPICAL SIZES VARY ON DIFFERENT MACHINES

BITS, BYTES, INTEGERS

CDataType Typical32-bit Typical64-bit x86-64

char 1 1 1

short 2 2 2

int 4 4 4

long 4 8 8

float 4 4 4

double 8 8 8

long double − − 10/16

pointer 4 8 8

• Data representations in bytes

Page 20: BITS, BYTES, AND INTEGERSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_07.pdf · BITS, BYTES, AND INTEGERS ... • Byte = 8 bits • Binary 00000000 2 to 11111111 2

{BIG, LITTLE} ENDIAN

BITS, BYTES, INTEGERS

• Data representations in bytes

• Suppose

• int *p = 0x100;• (*p) = 0x01234567;

Page 21: BITS, BYTES, AND INTEGERSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_07.pdf · BITS, BYTES, AND INTEGERS ... • Byte = 8 bits • Binary 00000000 2 to 11111111 2

{BIG, LITTLE} ENDIAN

BITS, BYTES, INTEGERS

• So which format does your computer use?

• Write a program which will show you the answer

Page 22: BITS, BYTES, AND INTEGERSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_07.pdf · BITS, BYTES, AND INTEGERS ... • Byte = 8 bits • Binary 00000000 2 to 11111111 2

{BIG, LITTLE} ENDIAN

BITS, BYTES, INTEGERS

• So which format does your computer use?

• Write a program which will show you the answer

Page 23: BITS, BYTES, AND INTEGERSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_07.pdf · BITS, BYTES, AND INTEGERS ... • Byte = 8 bits • Binary 00000000 2 to 11111111 2

{BIG, LITTLE} ENDIAN

BITS, BYTES, INTEGERS

• So which format does your computer use?

• Write a program which will show you the answer

Page 24: BITS, BYTES, AND INTEGERSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_07.pdf · BITS, BYTES, AND INTEGERS ... • Byte = 8 bits • Binary 00000000 2 to 11111111 2

{BIG, LITTLE} ENDIAN

BITS, BYTES, INTEGERS

Page 25: BITS, BYTES, AND INTEGERSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_07.pdf · BITS, BYTES, AND INTEGERS ... • Byte = 8 bits • Binary 00000000 2 to 11111111 2

WHAT ABOUT CHARACTERS?

BITS, BYTES, INTEGERS

Page 26: BITS, BYTES, AND INTEGERSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_07.pdf · BITS, BYTES, AND INTEGERS ... • Byte = 8 bits • Binary 00000000 2 to 11111111 2

WHAT ABOUT CHARACTERS?

BITS, BYTES, INTEGERS

Page 27: BITS, BYTES, AND INTEGERSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_07.pdf · BITS, BYTES, AND INTEGERS ... • Byte = 8 bits • Binary 00000000 2 to 11111111 2