22S-Assignment-5-Solution

# 22S-Assignment-5-Solution - CS107 Spring 2007 Handout 22S...

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

CS107 Handout 22S Spring 2007 May 4, 2007 Assignment 5 Solution Brought to you by Julie Zelenski and Jerry Cain. Problem 1: Binary numbers and bit operations A few examples are below. Checking your results in decimal is the easiest way to verify you’ve got it correct. 58 = 32 + 16 + 8 + 2 00111010 +47 = 32 + 8 + 4 + 2 + 1 + 00101111 105 01101001 = 64 + 32 + 8 + 1 = 105 100 = 64 + 32 + 4 01100100 -27 = 16 + 8 + 2 + 1 - 00011011 73 01001001 = 64 + 8 + 1 = 73 20 = 16 + 4 00010100 *3 = 2 + 1 * 00000011 60 00010100 00010100 00111100 = 32 + 16 + 8 + 4 = 60 Adding one to the maximum signed short (32767) gives the most negative short –32768. Adding one to the maximum unsigned short (65535) returns zero. In both cases, it is doing the usual binary arithmetic and carrying the last bit into the sign bit or off the end, wrapping back into range. No error is reported in either case. Assigning from larger to smaller type just truncates to the lower byte(s). For example, assigning the short 1025 to a char produces 1. The other direction has no problem or truncation. The remainder of a number when divided by 4 can be found in the bits in the places below 4 (i.e. the bits in the 2 and 1’s place). Thus we want to get the value of just those bits from the number. A bitwise AND can pull out a specific subset of bits, by AND’ing with something that has just those bits on. The integer 3 has only the 2 and 1 bits on, so if we take a number and bitwise AND it with 3, we can quickly compute its remainder when divided by 4: printf(“Remainder of %d is %d\n”, num, num & 3); If the upper byte of a short is all zero bits, then we can assign it to a char without data loss. Using a similar approach as above, we can extract just the high order bits from a number to see whether they are all zeros. The short with all the upper bits on is 32512 (32767-255). printf(“High byte of %d clear? %d \n”, num, (num & 32512) == 0));

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

View Full Document
2 There are easier and more portable ways to compute the number to AND against (using bitmask or bit shifting), but we won’t get into it for now. To change a number’s sign in two’s complement, we need to invert all the bits and add one. Inverting all the bits can be done with a bitwise XOR with that number that has all bits on. This operation will turn all bits previously off on and all bits on off, exactly what we want. The value with all bits on is –1. printf(“Start with %d, change sign %dn”, num, (num ^ -1) + 1); Here’s a simple encryption/decryption program that reads a file from standard input and writes the result to standard output. It encrypts every character with a constant key of ‘Z’. You could adapt it to encrypt by shorts or ints and using different keys if you felt like it. If you run this program twice on the same file, you will get the original contents back. #include <stdio.h>
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 01/14/2010 for the course CS 107 taught by Professor Cain,g during the Spring '08 term at Stanford.

### Page1 / 8

22S-Assignment-5-Solution - CS107 Spring 2007 Handout 22S...

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

View Full Document
Ask a homework question - tutors are online