DOC PREVIEW
Princeton COS 217 - Integral Data Types in C

This preview shows page 1-2-17-18-19-35-36 out of 36 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Integral Data Types in CSlide 2Goals for this LectureWhy Bits (Binary Digits)?Base 10 and Base 2Writing Bits is Tedious for PeopleRepresenting Colors: RGBFinite Representation of IntegersAdding Two Integers: Base 10Binary Sums and CarriesModulo ArithmeticOne’s and Two’s ComplementPutting it All TogetherSigned IntegersOverflow: Running Out of RoomBitwise Operators: AND and ORBitwise Operators: Not and XORBitwise Operators: Shift Left/RightData TypesC Integral Data TypesC Integral Data Types (cont.)The int Data TypeThe int Data Type (cont.)The unsigned int Data TypeThe long Data TypeThe unsigned long Data TypeThe short Data TypeThe unsigned short Data TypeThe signed char Data TypeThe unsigned char Data TypeThe char Data TypeThe char Data Type (cont.)Slide 33SummarySlide 35Slide 361Integral Data Types in C2•First lecture response sent out•Lots of good questions•Some questions will be punted to reading when appropriate•3 statements useful – found a few misunderstandings•Need to focus on statements, not just nouns•Still need pictures•Gedankenexperiment•What happens when you crank a stereo system to 11 (and beyond)?3Goals for this Lecture•Help you to learn about:•The binary number system•Why binary?•Converting between decimal and binary•The octal and hexadecimal number systems•Finite representations of binary integers•Unsigned integers•Integer addition•Signed integers•Bitwise operators•And, or, not, and xor•Shift-left and shift-right•The C integral data types•char, short, int, long (signed and unsigned)4Why Bits (Binary Digits)?•Computers are built using digital circuits•Inputs and outputs can have only two values•True (high voltage) or false (low voltage)•Represented as 1 and 0•Can represent many kinds of information•Boolean (true or false)•Numbers (23, 79, …)•Characters (‘a’, ‘z’, …)•Pixels•Sound•Can manipulate in many ways•Read and write•Logical operations•Arithmetic•…5Base 10 and Base 2•Decimal (base 10)•Each digit represents a power of 10•4173 = 4 x 103 + 1 x 102 + 7 x 101 + 3 x 100•Binary (base 2)•Each bit represents a power of 2•10110 = 1 x 24 + 0 x 23 + 1 x 22 + 1 x 21 + 0 x 20 = 22Decimal to binary conversion:Divide repeatedly by 2 and keep remainders12/2 = 6 R = 06/2 = 3 R = 03/2 = 1 R = 11/2 = 0 R = 1Result = 11006Writing Bits is Tedious for People•Octal (base 8)•Digits 0, 1, …, 7•Hexadecimal (base 16)•Digits 0, 1, …, 9, A, B, C, D, E, F0000 = 0 1000 = 80001 = 1 1001 = 90010 = 2 1010 = A0011 = 3 1011 = B0100 = 4 1100 = C0101 = 5 1101 = D0110 = 6 1110 = E0111 = 7 1111 = FThus the 16-bit binary number1011 0010 1010 1001converted to hex isB2A97Representing Colors: RGB•Three primary colors•Red•Green•Blue•Strength•8-bit number for each color (e.g., two hex digits)•So, 24 bits to specify a color•In HTML, on the course Web page•Red: <font color="#FF0000"><i>Symbol Table Assignment Due</i>•Blue: <font color="#0000FF"><i>Fall Recess</i></font>•Same thing in digital cameras•Each pixel is a mixture of red, green, and blue8Finite Representation of Integers•Fixed number of bits in memory•Usually 8, 16, or 32 bits•Unsigned integer•No sign bit•Always positive or 0•All arithmetic is modulo 2n•Examples of unsigned integers•00000001  1•00001111  15•00010000  16•00100001  33•11111111  2559Adding Two Integers: Base 10•From right to left, we add each pair of digits•We write the sum, and add the carry to the next column1 9 8+ 2 6 4SumCarry0 1 1+ 0 0 1SumCarry21614001011010Binary Sums and Carriesa b Sum a b Carry0 0 0 0 0 00 1 1 0 1 01 0 1 1 0 01 1 0 1 1 1XORAND 0100 0101 + 0110 0111 1010 11006910317211Modulo Arithmetic•Consider only numbers in a range•E.g., five-digit car odometer: 0, 1, …, 99999•E.g., eight-bit numbers 0, 1, …, 255•Roll-over when you run out of space•E.g., car odometer goes from 99999 to 0, 1, …•E.g., eight-bit number goes from 255 to 0, 1, …•Adding 2n doesn’t change the answer•For eight-bit number, n=8 and 2n=256•E.g., (37 + 256) mod 256 is simply 37•This can help us do subtraction…•Suppose you want to compute a – b•Note that this equals a + (256 -1 - b) + 112One’s and Two’s Complement•One’s complement: flip every bit•E.g., b is 01000101 (i.e., 69 in decimal)•One’s complement is 10111010•That’s simply 255-69•Subtracting from 11111111 is easy (no carry needed!)•Two’s complement•Add 1 to the one’s complement•E.g., (255 – 69) + 1  1011 1011 - 0100 0101 1111 1111 1011 1010bone’s complement13Putting it All Together•Computing “a – b”•Same as “a + 256 – b”•Same as “a + (255 – b) + 1”•Same as “a + onesComplement(b) + 1”•Same as “a + twosComplement(b)”•Example: 172 – 69•The original number 69: 0100 0101•One’s complement of 69: 1011 1010 •Two’s complement of 69: 1011 1011•Add to the number 172: 1010 1100•The sum comes to: 0110 0111•Equals: 103 in decimal 1010 1100 + 1011 1011 1 0110 011114Signed Integers•Sign-magnitude representation•Use one bit to store the sign•Zero for positive number•One for negative number•Examples•E.g., 0010 1100  44•E.g., 1010 1100  -44•Hard to do arithmetic this way, so it is rarely used•Complement representation•One’s complement•Flip every bit•E.g., 1101 0011  -44•Two’s complement•Flip every bit, then add 1•E.g., 1101 0100  -4415Overflow: Running Out of Room•Adding two large integers together•Sum might be too large to store in the number of bits available•What happens?•Unsigned integers•All arithmetic is “modulo” arithmetic•Sum would just wrap around•Signed integers•Can get nonsense values•Example with 16-bit integers •Sum: 10000+20000+30000 •Result: -553616Bitwise Operators: AND and OR•Bitwise AND (&)•Mod on the cheap!•E.g., h = 53 & 15;•Bitwise OR (|)&010 10 00 1|010 10 11 10 0 1 1 0 1 0 10 0 0 0 1 1 1 153& 150 0 0 0 0 1 0 1517Bitwise Operators: Not and XOR•One’s complement (~)•Turns 0 to 1, and 1 to 0•E.g., set last three bits to 0•x = x & ~7;•XOR (^)•0 if both bits are the same•1 if the two bits are different^010 10 11 018Bitwise Operators: Shift Left/Right•Shift left (<<): Multiply by powers of 2•Shift some # of bits to the left, filling the blanks with 0•Shift right (>>): Divide by powers of 2•Shift


View Full Document

Princeton COS 217 - Integral Data Types in C

Documents in this Course
Summary

Summary

4 pages

Lecture

Lecture

4 pages

Generics

Generics

14 pages

Generics

Generics

16 pages

Lecture

Lecture

20 pages

Debugging

Debugging

35 pages

Types

Types

7 pages

Lecture

Lecture

21 pages

Assembler

Assembler

16 pages

Lecture

Lecture

20 pages

Lecture

Lecture

39 pages

Testing

Testing

44 pages

Pipeline

Pipeline

19 pages

Lecture

Lecture

6 pages

Signals

Signals

67 pages

Building

Building

17 pages

Lecture

Lecture

7 pages

Modules

Modules

12 pages

Generics

Generics

16 pages

Testing

Testing

22 pages

Signals

Signals

34 pages

Lecture

Lecture

19 pages

Load more
Download Integral Data Types in C
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Integral Data Types in C and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Integral Data Types in C 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?