or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Syncing
xxxxxxxxxx
2018q1 Homework2 (assessment)
contributed by <
f74034067
>第 1 週測驗題 測驗 5
考慮以下整數乘法的實作程式碼:
上述
OP
對應到下方哪個程式敘述呢?(a)
ret = n << c;(b)
ret += n;(c)
ret <<= n;(d)
ret = c << n;(e)
ret += n << c;答案
(e)
ret += n << c;想法
首先看到
if (!(m % 2 - 1))
中如果 m 為奇數,也就是二進位表示最後一位為 1 時,則會進行OP
,再看到for
迴圈會不斷將 m / 2 ,也就是右移,而 c 會不斷累加,用以紀錄 m 右移了幾次,最後可以發現例如若 m = 11,則 ret = n * (1 + 2 + 8)
延伸
若是 m 為負數時,
m % 2
會得出 0 與 -1 兩種可能,所以(!(m % 2 - 1))
只會得出 false 的結果,因此 m < 0 時只會 return 0,乘法無法順利運作。如何修正?不要只有「因為…所以…」這樣的描述,要有推論和改進方案!我們做的是工程訓練,不是考公職。
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →第 2 週測驗題 測驗 3
考慮到某些實數的二進位表示形式如同 \(0.yyyyy...\) 這樣的無限循環小數,其中 \(y\) 是個 \(k\) 位的二進位序列,例如 \(\frac{1}{3}\) 的二進位表示為 \(0.01010101...\) (y =
01
),而 \(\frac{1}{5}\) 的二進位表示為 \(0.001100110011...\) (y =0011
),考慮到以下 y 值,求出對應的十進位分數值。010011
=> \(\frac{19}{X1}\)101
=> \(\frac{5}{X2}\)0110
=> \(\frac{2}{X3}\)想法
利用無窮等比級數的公式,當公比 -1 < r < 1,則總和為 \(\frac{首項}{1-r}\)
答案
010011
=> 首項 = \(\frac{19}{64}\) ,r = \(\frac{1}{64}\) => x1 = 63101
=> 首項 = \(\frac{5}{8}\) ,r = \(\frac{1}{8}\) => x2 = 70110
=> 首項 = \(\frac{6}{16}\) ,r = \(\frac{1}{16}\) => x3 = 5第 3 週測驗題 測驗 2
3 月 26 日的截止時間剩下幾小時了,但我看不到你的充分付出。
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →考慮到下方函式
shift_right_arith
和shift_right_logical
分別表示算術右移和邏輯右移,請嘗試補完程式碼。可由sizeof(int) * 8
得知整數型態int
的位元數w
,而位移量k
的有效範圍在 0 到w - 1
。想法
算術右移
sizeof(int) * 8
得知整數型態int
的位元數w
可推得 p1 = 3。if (x < 0)
可以推算當 x 最左邊的 bit 為 1 時 xsrl 必須把最左邊的 k 個 bits 改回 1 =>xsrl | mask
邏輯右移
xsra & ~mask
答案
延伸
延伸題目: 在 x86_64 和 Aarch32/Aarch64 找到對應的指令,並說明其作用和限制
Reference :
x86_64
sar dest, src
Arithmetic shift
dest
to the right bysrc
bits. Spaces are filled with sign bit (to maintain sign of original value), which is the original highest bit.shr dest, src
Logical shift
dest
to the right bysrc
bits.Aarch32/Aarch64
r0, r1, ASR r2
shift value in
r1
r2
places right and fill in vacant bits with copies of original most significant bitr0, r1, LSR r2
shift the binary value of
r1
r2
places to the right