LEC20081121

# LEC20081121 - Introduction to Computer Programming November...

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

Introduction to Computer Programming November 21, 2008 CSC180 Fall 2008, University of Toronto

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

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

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

View Full Document
Toggle a bit: /* toggle a bit at position p in n */ unsigned toggle(unsigned n, int p)
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 01/10/2011 for the course CSC 180 taught by Professor Na during the Fall '01 term at University of Toronto.

### Page1 / 8

LEC20081121 - Introduction to Computer Programming November...

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

View Full Document
Ask a homework question - tutors are online