CPSC120A
Fundamentals of Computer Science I

Post-lab 23

Binary

Do one of the following programming problems:

Smallest Power of 2

Write a function called largest_power(binary_string), which has one parameter: a string of binary digits. Your function should compute and return the smallest power of 2 that is greater than or equal to the integer represented by the input binary string.


Two's Complement

Write a function called twos_complement(binary_string) that has a single string parameter, binary_string, that consists entirely of the characters '0' and '1'. The function should return a new binary string that is the two's complement of binary_string. The two's complement of a binary number is one where all the bits to the left of the right-most 1 are flipped, all other bits are unchanged. To flip a bit, change it to the bit it is not. So to flip a 0, change it to a 1 and to flip a 1, change it to a 0.