學到一個小技巧: 若 n 是 32 bit 有號整數, n >> 31 等同於 n >= 0 ? 0 : -1。這才發覺負數的右移運算不會變零啊, 之前沒學好 XD
3 plus ones
Shared publicly•View activity
View 3 previous comments
- 有耶....Oct 13, 2011
- ANSI C in fact doesn't guarantee arithmetic shift on negative signed integers. Yet this pretty much works on all 2's complement machines:
http://en.wikipedia.org/wiki/Arithmetic_shift#fn%5F3%5FbackOct 13, 2011
- 這裡提到 left shift 的實作部份是什麼意思啊? http://gcc.gnu.org/onlinedocs/gcc-4.3.3/gcc/Integers-implementation.html#Integers-implementationOct 13, 2011
- : see
My takeaway: even if you're designing a very weird processor (DSP, GPU ...) you still want to to use 2's complement representation and make ">>" do arithmetic shift and "<<" do logical shift on negative signed integers.Oct 13, 2011
- How does Scott find the those document so quickly for every question or confusion we post on the board.....Oct 14, 2011
- : it's pretty simple: after bumping into a topic multiple times, I try to find a Google keyword that'll bring up the most useful references. I'm sure we've all been using this "associate a keyword with a topic" trick for years now in the age of Google.
For this thread I came up with the keywords "arithmetic shift", "shift undefined behavior". If we were discussing signed overflow, I'd Google "fwrapv".
I also only comment on topics that I'm already familiar with. You won't see me providing answers on threads about machine learning for example.Oct 14, 2011
Add a comment...