|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
# Q2. Binary
---
## Learning objectives
* Learn about bits and bytes, and find out how integers can be represented in a computer
* Solve a problem using what we have learnt from week 2, including:
* Conditional statements
* Loops
* Validating input from users
* Practise solving a programming question that is around the difficulty level of the first summative problem set
---
## Background information
(You need to use Visual Studio Code to see the images embedded in this page properly.)
[Bit](en.wikipedia.org/wiki/Bit) is the basic unit of information in a computer. It can only take two possible values, which can be considered as on/off, true/false, or 0/1. In a computer, data is represented using the binary numeral system - texts, numbers, pictures, audios, etc. are stored by a sequence of bits. [Byte](en.wikipedia.org/wiki/Byte) is a common unit of storage which consists of 8 bits.
Non-negative integers are commonly stored in 4 bytes (i.e. 32 bits). This allows us to store non-negative integers with values from 0 up to 4,294,967,295 (2^32 - 1). Below shows how integers 0 to 4 and 4,294,967,295 are stored in four bytes in a [big-endian](en.wikipedia.org/wiki/Endianness) system.
| Integer | Representation in a computer |
|:---------|:-----------------------------------:|
|0 | 00000000 00000000 00000000 00000000 |
|1 | 00000000 00000000 00000000 00000001 |
|2 | 00000000 00000000 00000000 00000010 |
|3 | 00000000 00000000 00000000 00000011 |
|4 | 00000000 00000000 00000000 00000100 |
|... | .... |
|4294967295| 11111111 11111111 11111111 11111111 |
The space among each byte is for illustration purposes only.
In this question, we convert a given non-negative integer from the decimal representation to the binary representation stored in a computer as illustrated above. Below we show how an integer can be represented in a binary numeral system.
In decimal representation, an integer `721` can be thought of as:
<img src="figs/721.png" height="30"/> <p>
Similarly, the number `101101` in binary representation can be thought of as:
<img src="figs/101101_1.png" height="30"/> <p>
(The subscript 2 represents that 101101 is in binary representation)
To convert the number `101101` from the binary representation to the decimal representation, we add all the numbers together:
<img src="figs/101101_2.png" height="30"/> <p>
(The subscript 10 represents that 45 is in decimal representation)
To convert the integer `45` from the decimal representation to the binary representation, we recursively divide the integer by 2 and get the remainder:
```
45/2 = 22 with remainder 1 (which represents the rightmost digit)
22/2 = 11 with no remainder
11/2 = 5 with remainder 1
5/2 = 2 with remainder 1
2/2 = 1 with no remainder
1/2 = 0 with remainder 1 (which represents the leftmost digit)
```
Therefore, the number `45` can be represented as:
<img src="figs/45.png" height="30"/> <p>
and the binary representation is `101101`, and in the 32-bit binary representation, it is `00000000000000000000000000101101`.
---
## Instruction
Consider a program doing the following:
> Get an integer from a user. Check if the integer is in the right range. If that is the case, convert it from the decimal representation to a 32-bit binary representation. If not, keep asking the user to provide another integer until a valid integer is provided. The printout from the program follows the ones from the `"Example"` section below.
1. Write down an integer satisfying each of the followings:
* Largest negative integer that is not in the right range: _____
* Smallest integer that is in the right range: _____
* Largest integer that is in the right range: ______
* Smallest positive integer that is not in the right range: _____
* The integer that will provide the printout with all zeros except the rightmost digit to be one: ____
* The integer that will provide the printout with all zeros except the second rightmost digit to be one: ____
* The integer that will provide the printout with all ones except the rightmost digit to be zero: ____
2. Write your code in [src/binary.py](src/binary.py) to implement the program specified above
3. Check your program using the integers listed in (1) as inputs (no submission required)
You are NOT allowed to use any functionality that we have not seen in the course. Essentially, you are not allowed to use:
* The built-in functions `bin()`, `len()`, `reverse()` or bitwise operators
* Containers like `tuple` and `list`
* string functions that we have not seen in the course
This question is relatively difficult.
* You may want to work with your classmates as a group to solve the problem
* Apply decomposition to break down the problem into smaller, more manageable sub-problems
* If you get stuck, feel free to ask your instructors or teachers for help. Alternatively, you can have a look at the hint file available on Moodle
---
## Assumptions
* Assume the input from the user must be a representation of an integer
---
## Example
```
Enter an integer (0...4294967295): -1
Sorry, -1 is not in the right range.
Enter an integer (0...4294967295): 45
Binary representation of 45 is 00000000000000000000000000101101.
```
Note here:
* `Enter an integer (0...4294967295): ` is the input prompt
* `Sorry, -1 is not in the right range.` and `Binary representation of 45 is 00000000000000000000000000101101.` are printouts
* `-1` and `45` are inputs from users
求问大神们 这个代码怎么写 感谢! |
|