{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info iconThis preview shows pages 1–12. 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 Document Right 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 Document Right 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 Document Right 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 Document Right Arrow Icon
Bitwise operators (2) bitwise OR ( | ) and AND ( & ) work bit-by- bit 01110001 | 10101010 = ? 11111011 01110001 & 10101010 = ? 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 Document Right Arrow Icon
Bitwise operators (4) bitwise NOT ( ~ ) also works bit-by-bit ~10101010 = ? 01010101 (duh) Substitute 0 for 1 and 1 for 0
Background image of page 10
Bitwise operators (5) Two other bitwise operators: bitwise left shift ( << ) bitwise right shift ( >> ) 00001111 << 2 = ?
Background image of page 11

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

View Full Document Right Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}