Bit manipulation

Bit manipulation is the act of algorithmically manipulating bits or other pieces of data shorter than a word. Computer programming tasks that require bit manipulation include low-level device control, error detection and correction algorithms, data compression, encryption algorithms, and optimization. For most other tasks, modern programming languages allow the programmer to work directly with abstractions instead of bits that represent those abstractions. Source code that does bit manipulation makes use of the bitwise operations: AND, OR, XOR, NOT, and bit shifts.

Bit manipulation, in some cases, can obviate or reduce the need to loop over a data structure and can give many-fold speed ups, as bit manipulations are processed in parallel, but the code can become more difficult to write and maintain.

At the heart of bit manipulation are the bit-wise operators & (and), | (or), ~ (not) and ^ (exclusive-or, xor) and shift operators a << b and a >> b.

There is no boolean operator counterpart to bitwise exclusive-or, but there is a simple explanation. The exclusive-or operation takes two inputs and returns a 1 if either one or the other of the inputs is a 1, but not if both are. That is, if both inputs are 1 or both inputs are 0, it returns 0. Bitwise exclusive-or, with the operator of a caret, ^, performs the exclusive-or operation on each pair of bits. Exclusive-or is commonly abbreviated XOR.

Set union A | B
Set intersection A & B
Set subtraction A & ~B
Set negation ALL_BITS ^ A or ~A
Set bit A |= 1 << bit
Clear bit A &= ~(1 << bit)
Test bit (A & 1 << bit) != 0
Extract last bit A&-A or A&~(A-1) or x^(x&(x-1))
Remove last bit A&(A-1)
Get all 1-bits ~0

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市返十,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌补憾,老刑警劉巖麦射,帶你破解...
    沈念sama閱讀 221,198評論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件暇赤,死亡現(xiàn)場離奇詭異王污,居然都是意外死亡,警方通過查閱死者的電腦和手機棒妨,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評論 3 398
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來含长,“玉大人券腔,你說我怎么就攤上這事伏穆。” “怎么了纷纫?”我有些...
    開封第一講書人閱讀 167,643評論 0 360
  • 文/不壞的土叔 我叫張陵枕扫,是天一觀的道長。 經(jīng)常有香客問我辱魁,道長铡原,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,495評論 1 296
  • 正文 為了忘掉前任商叹,我火速辦了婚禮燕刻,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘剖笙。我一直安慰自己卵洗,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 68,502評論 6 397
  • 文/花漫 我一把揭開白布弥咪。 她就那樣靜靜地躺著过蹂,像睡著了一般。 火紅的嫁衣襯著肌膚如雪聚至。 梳的紋絲不亂的頭發(fā)上酷勺,一...
    開封第一講書人閱讀 52,156評論 1 308
  • 那天,我揣著相機與錄音扳躬,去河邊找鬼脆诉。 笑死,一個胖子當(dāng)著我的面吹牛贷币,可吹牛的內(nèi)容都是我干的击胜。 我是一名探鬼主播,決...
    沈念sama閱讀 40,743評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼役纹,長吁一口氣:“原來是場噩夢啊……” “哼偶摔!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起促脉,我...
    開封第一講書人閱讀 39,659評論 0 276
  • 序言:老撾萬榮一對情侶失蹤辰斋,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后瘸味,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體宫仗,經(jīng)...
    沈念sama閱讀 46,200評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,282評論 3 340
  • 正文 我和宋清朗相戀三年硫戈,在試婚紗的時候發(fā)現(xiàn)自己被綠了锰什。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,424評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖汁胆,靈堂內(nèi)的尸體忽然破棺而出梭姓,到底是詐尸還是另有隱情,我是刑警寧澤嫩码,帶...
    沈念sama閱讀 36,107評論 5 349
  • 正文 年R本政府宣布誉尖,位于F島的核電站,受9級特大地震影響铸题,放射性物質(zhì)發(fā)生泄漏铡恕。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,789評論 3 333
  • 文/蒙蒙 一丢间、第九天 我趴在偏房一處隱蔽的房頂上張望探熔。 院中可真熱鬧,春花似錦烘挫、人聲如沸诀艰。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,264評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽其垄。三九已至,卻和暖如春卤橄,著一層夾襖步出監(jiān)牢的瞬間绿满,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,390評論 1 271
  • 我被黑心中介騙來泰國打工窟扑, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留喇颁,地道東北人。 一個月前我還...
    沈念sama閱讀 48,798評論 3 376
  • 正文 我出身青樓辜膝,卻偏偏與公主長得像无牵,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子厂抖,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,435評論 2 359

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,554評論 0 23
  • 浪過濤破如心碎, 江流盡去誓不回克懊。 此水含恨走千里忱辅, 試問王孫歸不歸。
    很高很瘦閱讀 285評論 0 0
  • 能夠遇見你谭溉,對我來說是最大的幸福墙懂。有了你,我的生活變得無限寬廣扮念,有了你世界變得如此迷人损搬,你是世界,世界是你。 ...
    小包鑫閱讀 275評論 0 2