C_lecture_8

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

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

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

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

View Full Document
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
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?

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

View Full Document
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
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 )

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

View Full Document
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
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: ! )

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

View Full Document
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
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)

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

View Full Document
± bitwise NOT ( ~ ) also works bit-by-bit ± ~10101010 = ? ± 01010101
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
Ask a homework question - tutors are online