推 suhorng:少打了一個 "!" 09/17 21:09
→ bleed1979:!(x & (1 << i) 和 i+1 果然是短碼達人... 09/17 21:12
→ bleed1979:不過shift的次數有討論的空間... 09/17 21:15
推 VictorTom:小弟比較喜歡反方向說XD 09/17 21:40
→ VictorTom:i=0; for(i=0; x; ++i, x>>=1); return i; 這樣:) 09/17 21:40
→ VictorTom:不過前題是x必須是正值, 不然迴圈就停不下來了....Orz 09/17 21:41
推 VictorTom:不喜歡迴圈的話還可以搞個遞迴版的來算....XDDD 09/17 21:46
→ adrianshum:明顯我老眼昏花了 XDD btw, 我的寫法旨在簡化每個 09/17 22:13
→ adrianshum:iteration 所做的東西, 不然又 <<=1 又 -- 蠻混亂的 09/17 22:14
推 VictorTom:既然如此, 小弟就一定要推一下遞迴版的了.... 09/17 22:21
→ VictorTom:int binaryLength(unsigned int x, int i=0) { 09/17 22:22
→ VictorTom: return x>0 ? binaryLength(x>>1, i+1) : i; } 09/17 22:22
→ VictorTom:這個寫法~~~~根本就是來亂在惡搞的....XDDD 09/17 22:23
→ adrianshum:遞迴版好物! 09/17 22:48