!!
節錄部份
作者 Barros (Leila) 站內 Programming
標題 [問題] 新手發問 "!!"的意思
時間 Thu Mar 8 21:11:04 2012
───────────────────────────────────────
看資料結構的書的時候,應該是用c寫的吧
發現code裡出現 !! 這個符號
煩請解惑感激不盡~
節錄部份code:
BinQueue
Merge(BinQueue H1, BinQueue H2)
{
Bintree T1, T2, Carry=Null;
int i j;
if(H1->CurrentSize +H2->CurrentSize >Capacity)
error("Merge would exceed capacity");
H1->CurrentSize += H2->CurrentSize;
for(i=0, j=1; j<= H2->CurrentSize; i++, j*=2)
}
T1=H1->TheTrees[i];T2=H2->TheTrees[i];
switch( !!T1 + 2 * !! T2 + 4 * !! Carry)
{
case 0: /*no trees*/
case 1: /*only H1*/
break;
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 219.84.2.1
→ Barros:因為google過了也找不到 只好來這裡發問了 219.84.2.1 03/08 21:12
→ tropical72:是長得像這樣嗎?int a=5; if(!!a).. 123.195.165.40 03/08 21:13
→ tropical72:簡單的說,a是非0的話,!!a會傳回1;否則 123.195.165.40 03/08 21:14
→ tropical72:傳回0。 123.195.165.40 03/08 21:14
※ 編輯: Barros 來自: 219.84.2.1 (03/08 21:29)
→ s66449:加了兩個not 不就等同互相抵消了?? 114.27.187.150 03/08 22:13
→ s66449:有加和沒加是一樣的? 114.27.187.150 03/08 22:13
→ tropical72:不同唷,a=1,b=5而言 : 123.195.165.40 03/08 22:19
→ tropical72:a+=b; --> a+=5 ---> a=6; 123.195.165.40 03/08 22:20
→ tropical72:a+=!!b ---> a+=!(!5)-->a+=!0-->a+=1 123.195.165.40 03/08 22:20
→ tropical72:--> a=2; 123.195.165.40 03/08 22:20
→ tropical72:(T1!=0)|((T2!=0)<<1)|((C!=0)<<2) 123.195.165.40 03/08 22:26
→ Barros:了解了,謝謝 219.84.58.43 03/08 22:47
→ MOONRAKER:真是神祕。 61.231.8.206 03/09 08:46
推 yauhh:一個!將整數變成布爾值,但邏輯意思是反的, 180.206.52.95 03/09 18:42
→ yauhh:再加!就回來了. 180.206.52.95 03/09 18:43
→ yauhh:他做二件事,一是將整數對應為0,1,二是將0,1 218.160.108.73 03/09 21:56
→ yauhh:(true,false)反過來當作數字來用. 但例子中 218.160.108.73 03/09 21:57
→ yauhh:看起來作法好怪,邏輯運算當數字運算用,數字 218.160.108.73 03/09 21:57
→ yauhh:運算卻當位元運算用. 218.160.108.73 03/09 21:58
作者 LPH66 (-858993460) 看板 Programming
───────────────────────────────────────
這是懶人用switch...
!! 是做兩次 boolean not
所以非零的會變成 1 是零的就還是 0
於是 !!T1 + 2 * !! T2 + 4 * !! Carry 這個算式
將三件事 (T1 != 0, T2 != 0, Carry != 0) 編碼成一個整數
若三者都為 0 則它會算出二進位的 000 = 十進位 0
若只有 T1 非 0 則它會算出二進位的 001 = 十進位 1
若只有 T2 非 0 010 = 十進位 2
若只有 Carry 非 0 100 = 十進位 4
等等
這樣就能以 switch 一次判斷三個條件的真假
你可以注意到這個 switch 裡的 case 有註解寫說這是什麼情況
就是這麼來的
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.28.91
→ popcorny:我是覺得最起碼要用Macro包一下 114.32.239.120 03/11 14:20
→ popcorny:#define ISTRUE(x) (!!(x)) 114.32.239.120 03/11 14:21
→ popcorny:可讀性跟你說的其實沒有那麼衝突 114.32.239.120 03/11 14:22
作者 adrianshum (Alien) 看板 Programming
───────────────────────────────────────
不是覺得這種精簡寫法一定有錯,但同樣做法,真的要寫得那麼
非直覺嗎?為什麼不可以寫成:
int t = 0;
if (T1) t |= 1;
if (T2) t |= 2; // 或寫成 t |= (1<<1);
if (Carry) t |= 4; // t |= (1 << 2)
switch (t) {
//..
}
再不然,寫成
int t = (T1? 1 : 0) | (T2 ? 2 : 0) | (Carry ? 4 : 0);
(以上 bitwise OR 可改用 +,看你自己喜好)
這樣單從 code 表面上就看得出是根據 T1, T2, Carry 來 turn-on
各個 bit。除非真的程式慢得連這一點點 performance 都要壓搾,
不然至少對我來說,上面的寫法比 !!T1 + 2 * !! T2 + 4 * !! Carry
看起來合理得多了
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 223.19.47.36
2012年3月13日 星期二
2011年4月1日 星期五
exchange the values of two variables
Let us consider another example. Usually when we want to exchange the values of
two variables, we do the following.
Another way of doing this is as follows.
two variables, we do the following.
temp = a;
a = b;
b = temp;
Another way of doing this is as follows.
b ^= a ^= b ^= a;
2010年12月24日 星期五
效能 速度 近似
Bit Twiddling Hacks
By Sean Eron Anderson
seander@cs.stanford.edu
http://www-graphics.stanford.edu/~seander/bithacks.html
Fast and accurate sine/cosine
http://www.devmaster.net/forums/showthread.php?t=5784
A Guide to Approximations
Jack G. Ganssle
jack@ganssle.com
http://www.ganssle.com/approx/approx.pdf
fdlibm
http://www.netlib.org/fdlibm/
Elementary Functions and Calculators
Richard Parris, Phillips Exeter Academy
http://www.swarthmore.edu/NatSci/smaurer1/Math6B/Readings/calculatorsParris-rev.pdf
A Fast, Vectorizable Algorithm for Producing
Single-Precision Sine-Cosine Pairs
Marcus H. Mendenhall
http://arxiv.org/PS_cache/cs/pdf/0406/0406049v1.pdf
http://www.smooth.url.tw/wiki/doku.php?id=sorting
By Sean Eron Anderson
seander@cs.stanford.edu
http://www-graphics.stanford.edu/~seander/bithacks.html
Fast and accurate sine/cosine
http://www.devmaster.net/forums/showthread.php?t=5784
A Guide to Approximations
Jack G. Ganssle
jack@ganssle.com
http://www.ganssle.com/approx/approx.pdf
fdlibm
http://www.netlib.org/fdlibm/
Elementary Functions and Calculators
Richard Parris, Phillips Exeter Academy
http://www.swarthmore.edu/NatSci/smaurer1/Math6B/Readings/calculatorsParris-rev.pdf
A Fast, Vectorizable Algorithm for Producing
Single-Precision Sine-Cosine Pairs
Marcus H. Mendenhall
http://arxiv.org/PS_cache/cs/pdf/0406/0406049v1.pdf
http://www.smooth.url.tw/wiki/doku.php?id=sorting
訂閱:
文章 (Atom)