?? l41.1a
字號:
#printThe problem is to produce a function bitct(x)which examines the bits in x, returning a count ofthe number of 1-bits. There are various ways of doingthis job: here are two.(1) a sane way. Shift the word x right 16 times (you areon UNIX) and check the rightmost bit each time, countingthe number of times it is '1'.(2) a machine-independent (sort of) way. The logicalbitwise AND of x and x-1 contains one fewer one bit than x itself.Loop anding x and x-1 until you get zero.Program either algorithm. Compile and test it. Leave it ona file bitct.c and type "ready".#once #create tzaqc.cmain(){ int x; x=23069; if (bitct(x) != goodct(x)) return(1); x=0; if (bitct(x) != goodct(x)) return(1); x=16384; if (bitct(x) != goodct(x)) return(1); x = -1; if (bitct(x) != goodct(x)) return(1); x= -200; if (bitct(x) != goodct(x)) return(1); return(0);}goodct(x){ int k, i; for(k=i=0; i<16; i++) { k =+ (x&1); x= x>>1; } return(k);}#usercc tzaqc.c bitct.oa.out#succeed/* a possible solution */bitct(x){ int k, i; for(i=k=0; i<16; i++) { if (x&1) k++; x >>= 1; } return(k);}/* by the way, if you really care aboutthis problem a table lookup by whole bytesis faster */#log#next42.1a 10
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -