Binary Numbers

Problem #88

Tags: computing instructional

Who solved this?

Previous:Product Rule and Trigonometric Derivatives Next:Hexadecimal Numbers


As many of us are taught in grade school, all positive integers can be expressed using some combination of only nine digits, those digits of course being 0 1 2 3 4 5 6 7 8 9. If we need to count any number higher than our greatest digit 9, then we just increase the digit to the left instead (imagining an invisible leading 0 if no digit is present) and then reset the current digit to 0. Assuming you have learned how to count to 10 some time ago, this may not appear profound.

However, the fact that we use 10 digits is mostly arbitrary. This Base-10 or Decimal system you may have also heard that computers can only process numbers as 1s and 0s. This is a physical limitation of electrical wires either being in a "high-voltage" or "low-voltage" state. Since there are only 2 possible states (and therefore only two possible digits), this is called a Base-2 or Binary system. At first this may seem strange, but realize that we can follow the same rules as we defined for our Decimal system in order to again represent any positive integer.

Let's walk through an example counting in Binary. We start at 1, but then since we have no larger digit, we instead increase the digit to the left (adding a 0 if no digit is present) and reset our current digit to 0, yielding 10 which is the Binary representation of the Decimal number 2. Following this logic, we can work out that the first few Binary numbers are 1, 10, 11, 100, 101, 110, ...

Maybe you can predict how this has potential cause confusion. If we are presented with the number 101, how are we supposed to know if it represents the Base-10 value 101, or the Base-2 value for 5? In computer science it is common to see Binary numbers prefixed with the characters 0b to indicate explicitly that they should be interpreted as Binary. And so while 101 may be ambiguous, 0b101 is clearly understood as a Binary representation.

Problem Statement

Input Data
First line will be D, the quantity of Decimal testcases.
D lines will then follow, each containing a single integer in Base-10 which you will be expected to convert into Base-2.
The next line will then be B, the quantity of Binary testcases.
B lines will then follow, each containing a single integer in Base-2 which you will be expected to convert into Base-10.

Answer
Should consist of D space-separated values followed by B space-separated values, corresponding to the converted testcases.

Example

input data:
2
10
100
2
0b1010
0b10101

answer:
0b1010 0b1100100 10 21
You need to login to get test data and submit solution.