C_lecture_8 - CS 11 C track lecture 8 Last week hash tables...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 11 C track: lecture 8 ± Last week: hash tables, C preprocessor ± This week: ± Other integral types: short , long , unsigned ± bitwise operators ± switch ± "fun" assignment: virtual machine
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Integral types (1) ± Usually use int to represent integers ± But many other integral (integer-like) types exist: ± short ± long ± char ± unsigned int ± unsigned short ± unsigned long ± unsigned char
Background image of page 2
Integral types (2) ± Two basic things that can vary: ± unsigned vs. signed (default) ± length: char , short , int , long ± Note that char is an integral type ± can always treat char as an 8-bit integer ± Two basic questions: ± Why use unsigned types? ± When should we use shorter/longer integral types?
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Integral types (2) ± Why use unsigned types? ± may be used for something that can't be negative ± e.g. a length ± gives you 2x the range due to last bit ± may want to use it as an array of bits ± so sign is irrelevant ± C has lots of bitwise operators
Background image of page 4
Integral types (3) ± When should we use shorter/longer integral types? ± to save space when we know range is limited ± when we know the exact number of bits we need ± char always 8 bits ± short usually 16 bits ± int usually 32 bits (but sometimes 64) ± long usually 32 bits (but sometimes 64) ± guaranteed: length( char ) < length( short ) <= length ( int ) <= length( long )
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Integral types (4) ± unsigned by itself means unsigned int ± Similarly it's legal to say ± short int ± unsigned short int ± long int ± unsigned long int ± but usually we shorten by leaving off the int
Background image of page 6
Bitwise operators (1) ± You don't need to know this for this lab! ± But a well-rounded C programmer should know this anyway. .. ± There are several "bitwise operators" that do logical operations on integral types bit-by-bit ± OR ( | ) (note difference from logical or: || ) ± AND ( ) (note difference from logical and: ) ± XOR ( ^ ) ± NOT ( ~ ) (note difference from logical not: ! )
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Bitwise operators (2) ± bitwise OR ( | ) and AND ( ) work bit-by- bit ± 01110001 | 10101010 = ? ± 11111011 ± = ? ± 00100000 ± NOTE: They don't do short-circuit evaluation like logical OR ( || ) and AND ( ) do ± because that wouldn't make sense
Background image of page 8
Bitwise operators (3) ± bitwise XOR ( ^ ) also works bit-by-bit ± 01110001 ^ 10101010 = ? ± 11011011 ± Bit is set if one of the operand's bits is 1 and the other is 0 (not both 1s or both 0s)
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
± bitwise NOT ( ~ ) also works bit-by-bit ± ~10101010 = ? ± 01010101
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 63

C_lecture_8 - CS 11 C track lecture 8 Last week hash tables...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online