LEC20081121 - Introduction to Computer Programming CSC180...

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

View Full Document Right Arrow Icon
Introduction to Computer Programming November 21, 2008 CSC180 Fall 2008, University of Toronto
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
Bitwise operators continued Decimal to binary: #define UNSIGNED_INT_BITS (8 * sizeof(unsigned)) #define SHIFT_AMOUNT (UNSIGNED_INT_BITS - 1) void dec2bin(unsigned n) { int i; for (i = 0; i < UNSIGNED_INT_BITS; i++, n <<= 1) printf("%d", n >> SHIFT_AMOUNT); printf("\n"); } /* recursive version, slow, don’t use often */ void binary(unsigned n) CSC180 Fall 2008, University of Toronto 1
Background image of page 2
{ static count = UNSIGNED_INT_BITS; if (--count < 0) { count = UNSIGNED_INT_BITS; return; } binary(n >> 1); printf("%d", n & 01); } Swap two numbers without a temporary variable: #define swap(x, y) ((x) ^= (y), (y) ^= (x), (x) ^= (y)) Exercise: prove that the above macro swap works. Note that for any bit x , x 0 = x , x 1 = x , and x x = 0 , where denotes XOR . CSC180 Fall 2008, University of Toronto 2
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
Toggle a bit: /* toggle a bit at position p in n */ unsigned toggle(unsigned n, int p)
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.

{[ snackBarMessage ]}

Page1 / 8

LEC20081121 - Introduction to Computer Programming CSC180...

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