lecture08

# lecture08 - Last time Introduction to Low-Level Programming...

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

Programming Concepts CMSC 212 ecture 8 1 CMSC 212 Lecture 8 September 24, 2009 Last time Chapter 5, Operators and expressions – the conditional expression and the comma operator – representing numbers in various bases (binary, hex, decimal, and octal) and converting between them – the bit shift operators – the bitwise operators ( , | , ^ ) 2 CMSC 212 Chapter 5, Operators and Expressions, con't. 3 CMSC 212 Printing a number in binary One way to print the binary representation of a decimal number: – If the number is odd, its rightmost or low-order bit will be 1, otherwise its rightmost bit will be 0. – The number % 2 will be 0 if the number is even, meaning its last bit is 0, so print a 0 in this case. Otherwise print a 1. – Get rid of the last bit by dividing the number by 2, or by shifting it 4 CMSC 212 one bit to the right. – Continue until the number is 0. void print_binary(unsigned int n) { while (n > 0) { if (n % 2 == 0) printf("0"); else printf("1"); n= n >> 1; } Call: print_binary(11); Output: 1101 Problem? (Think about ways this can be fixed.)

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## lecture08 - Last time Introduction to Low-Level Programming...

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

View Full Document
Ask a homework question - tutors are online