4 Introduction to C

4 Introduction to C - !"#$"#% CMSC 216...

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

View Full Document Right Arrow Icon
!"#$"## # CMSC 216 Introduction to Computer Systems Lecture 4 Introduction to C and arrays Jan Plane & Pete Keleher {jplane, keleher}@cs.umd.edu Administrivia • Project 1 questions – see TAs or instructors in office hours • Read Chapter 5 of Reek – parts of Chapters 8 and 9 covered today and Tuesday
Background image of page 1

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

View Full DocumentRight Arrow Icon
!"#$"## ! Working with other bases printf() has format specifiers to allow printing of values in hex or octal (not binary, though) %x / %X : hexadecimal ( a-f / A-F ) %o : octal • " %08x " often used for printing unsigned int s as zero-padded, 8-digit hex numbers • There also exist similar mechanisms for reading in hexadecimal and octal representations of numbers using scanf() • But remember, all values are stored in binary, regardless of what base the numeric literal is! 3 Bit operations Numbers are represented using a fixed number of bits – typically a char is 8 bits, an int is 32 bits, a long is 32 or 64 bits C permits direct manipulation of the bits within a number – this is powerful and allows you to do exactly what you want – these can be nonportable: it's easy to write programs that don't work the same on different platforms – usually unsigned integers are used for bitwise operations An unsigned char as a series of bits: 4 1 1 0 1 0 1 1 0 leftmost (or high-order) bit rightmost (or low-order) bit
Background image of page 2
!"#$"## Shifting operators • The << and >> operators shift a value a given number of bits to the left or right, respectively • Should only be used with unsigned integer as left operand • Right operand must be between 0 and (# of bits of left operand) - 1 • Zero bits replace the vacated bits • Examples: unsigned int a = 0x55555555; /* 0101 . .. */ printf("a << 2: %08x\n", a << 2); printf("a >> 3: %08x\n", a >> 3); printf("a: %08x\n", a); 5 Bitwise operators • We can use the logical operations of AND, OR, NOT, and XOR on the bits of numbers, using bitwise operators • Bitwise AND: & • Bitwise OR: | • Bitwise NOT (unary): ~ • Bitwise XOR: ^ 6 & 0 1 0 0 0 1 0 1 | 0 1 0 0 1 1 1 1 ^ 0 1 0 0 1 1 1 0
Background image of page 3

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

View Full DocumentRight Arrow Icon
!"#$"## ' Bitwise operator examples unsigned int a = 0x5555ffff, b = 0xaaaa1111; unsigned int ones = 0; ones = ~ones; printf("a AND b: %08x\n", a & b); printf("a AND 0: %08x\n", a & 0); printf("a AND ones: %08x\n", a & ones); printf("a OR b: %08x\n", a | b);
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 14

4 Introduction to C - !&quot;#$&quot;#% CMSC 216...

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

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