contributed by < RusselCK
>
RusselCK
Output
FP64, FP32, FP16, BFLOAT16, TF32, and other members of the ZOO
根據測試結果,我們可知 fp32tobf16()
的功能相當於 將 Fraction 0 捨 1 入 到小數點第 7 位
程式碼解析: ( 可與 quiz5 Q2 比較 )
*py
= (int *) &y
: 將 &y
轉換為 指向 int 型態的指標*py
與 y
指向同一塊記憶體,但看法不同 )exp
: *py
Exponent 部份
man
: *py
Fraction ( Mantisa ) 部份
為何這裡的 mask 後面要加 u
,但 quiz5 Q2. 的卻不用?
是否能改成 unsigned int *py = (int *) &y
,mask 後面就不用加 u
了呢?
exp
= 0man
= 0exp
= ( 0 11111111 0000…0000 ) = 0x7F800000y
的 Fraction 0 捨 1 入 到小數點第 7 位
r
應為 ( * ******** 0000000 1000 0000 0000 0000 )#15
: 保留 Sign 與 Exponent
#16
: 除以 = 256do{...}while(0)
的考量展開 macro
#12
的 ;
,#13
的 else
會找不到對應的 if
使用 do{...}while(0)
並展開 macro
TODO : 2. 避免 goto
output
根據 C 語言規格書 6.5.2.5 Compound literal:
A postfix expression that consists of a parenthesized type name followed by a braceenclosed list of initializers is a compound literal. It provides an unnamed object whose value is given by the initializer list.
在括號(()
)中為型別名稱,並接續 braces({}
) 中的初始化 list 會行成一個 compound literal。這讓我們可以定義一個匿名的物件,並指派給某一變數或者作為 function 的參數使用。
If the type name specifies an array of unknown size, the size is determined by the initializer list as specified in 6.7.8, and the type of the compound literal is that of the completed array type. Otherwise (when the type name specifies an object type), the type of the compound literal is that specified by the type name.
nums
containing n + 1
integers where each integer is in the range [1, n]
inclusive.nums
, return this duplicate number.根據題目條件,我們可以知道下面兩件事:
nums
index 必為 ~ 各出現 1 次nums
的內容 必為 ~ 其中 1 個數字出現多次,其餘只出現 0 或 1 次考慮 二進制 ( 以 0 ~ 7 為例 )
Decimal | Binary |
---|---|
0 | 000 |
1 | 001 |
2 | 010 |
3 | 011 |
4 | 100 |
5 | 101 |
6 | 110 |
7 | 111 |
假設 nums[8] = {1, 2, 3, 4, 5, 5, 6, 7}
我們可以發現,在重複數字中為 1 的 bit,nums 加總 比 index 加總 還多
程式碼解析:
log_2
: 只少要檢查幾個 bits ( i.e. 最大的 1 在 第幾個 bit )8 * sizeof(int)
而非 32
?
sizeof(int)
不同逐一檢查每個 bit :
c1
: index 的加總c2
: nums 內容 的加總#14
、#15
: 若 c1 < c2
,代表該 bit 在重複數字中也為 1,要加進 res
c1 < c2
效能評估:
null
.next
pointer. Internally, pos
is used to denote the index of the node that tail's next
pointer is connected to.
pos
is not passed as a parameter.low
走 步, fast
就走 步low
、fast
相遇了,代表 fast
比 low
多走了 圈,假設 Cycle 起點 與 第一次相遇點 差距
head
與 Cycle 起點 差距
head
與 第一次相遇點 差距
令 low
從 head
出發、 fast
從 第一次相遇點 出發
且 兩者前進速度改為相同
low
會停在 Cycle 起點,與 head
差距 fast
會停在 與 head
差距 low
、fast
兩者差距為