LEC20081120

LEC20081120 - Introduction to Computer Programming CSC180...

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

Introduction to Computer Programming November 20, 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 Applicable to char, short, int, and long, signed or unsigned. In decreasing precedence, the bitwise operators are ~ one’s complement (unary) << >> left shift right shift bitwise AND ^ bitwise XOR (exclusive-or) | bitwise OR Example: let a patchwork be deﬁned by f ( x, y ) = (( x y y - 350) ± 3)) 2 , where x is row index and y is column index. If ( f ( x, y ) ± is 1, paint the dot at ( x, y ) white, otherwise paint the dot at ( x, y ) black. CSC180 Fall 2008, University of Toronto 1
Code for the above example: /* patchwork.c -- designed by S. Sleator, 1976 */ #include <stdio.h> int f(int x, int y) { int t; t = (x ^ ~y) & ((y - 350) >> 3); t = t * t; return (t >> 12) & 1; } #define X 300 #define Y 300 int main() { int i, j; CSC180 Fall 2008, University of Toronto 2

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

View Full Document
for (j = 0; j < Y; j++) printf("%c", f(i, j) == 0 ? ’0’ : ’1’); printf("\n"); } return 0; } A patchwork produced using the above program: Exercise: let f ( x, y ) = ( x 2 y ± , 1 x, y 256 . Paint the dot at
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 11

LEC20081120 - Introduction to Computer Programming CSC180...

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

View Full Document
Ask a homework question - tutors are online