神奇的位运算

Last update on July 13, 2014.

1。计算一个整数的二进制中1的个数
2。计算一个整数的二进制中末尾0的个数
3。判断一个整数是否是2的n次幂
4。计算比一个整数大的最小的2的n次幂
5。交换两个整数
6。计算一个整数的绝对值
7。计算两个整数中的较小值和较大值
计算一个整数的二进制中1的个数
unsigned int v; // 整数
unsigned int c; // 运算结果
for (c = 0; v; c++){
    v &= (v - 1); // 清空最低的1比特位,v&-v即是最低的比特位,所以这句也可以写成v-=(v&-v)
}
计算一个整数的二进制中末尾0的个数
unsigned int v;  // 整数

int r;           // 运算结果

static const int MultiplyDeBruijnBitPosition[32] ={

  0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,

  31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9

};

r = MultiplyDeBruijnBitPosition[((uint32_t)((v & -v) * 0x077CB531U)) >> 27];
复杂度O(c)5步
神奇的0x077CB531U与MultiplyDeBruijnBitPosition数组
判断一个整数是否是2的n次幂
unsigned int v;
bool ispow = (v&(v-1))==0;
计算比一个整数大的最小的2的n次幂
例如比9大的最小的2的n次幂是16,比7大的2最小的2的n次幂是8
unsigned int v; //整数
v--;
v |= v>>1;
v |= v>>2;
v |= v>>4;
v |= v>>8;
v |= v>>16;
v++;
交换两个整数
#define SWAP(a,b) ((a^=b),(b^=a),(a^=b))
计算一个整数的绝对值
int v;          // 一个整数
unsigned int r; // 结果
int const mask = v>> sizeof(int) * CHAR_BIT - 1;
r = (v+mask)^mask;

计算两个整数中的较小值和较大值
int x, y;
int r;
r = y ^ ((x^y) & -(x<y)); // min(x,y)
r = x ^ ((x^y) & -(x<y)); // max(x,y)

Next entry

Previous entry

Similar entries

Pingbacks

Pingbacks are open.

Trackbacks

  1. ipage $1 hosting promo on 02/10/2016 5:37 a.m. #

    Zinnia's Weblog - 神奇的位运算

  2. selaput dara buatan on 02/11/2016 1:10 a.m. #

    Zinnia's Weblog - 神奇的位运算

  3. mobitrans.net on 02/11/2016 7:08 a.m. #

    Zinnia's Weblog - 神奇的位运算

  4. how to get coins in fifa 13 on 02/11/2016 10:28 p.m. #

    <a href="http://www.hplovecraft.com/news/bPsjc7QMce.html">fifa 13 ultimate team coins</a>

  5. cosmetics mac on 02/13/2016 4:05 a.m. #

    Zinnia's Weblog - 神奇的位运算,cosmetics mac

  6. pettshirts on 02/13/2016 9:07 a.m. #

    Zinnia's Weblog - 神奇的位运算

  7. maccosmeticswholesalers mac false eyelashes c 3 on 02/14/2016 4:02 p.m. #

    <a href="http://www.destin-8.co.uk/upload/maccosmeticswholesaleoutlet_urban_decay_makeup_c_3.pdf">maccosmeticswholesaleoutlet urban decay makeup c 3</a> <a href="http://www.parasoloutlet.co.uk/upload/maccosmeticswholesaleoutlet_hello_kitty_series_c_8.pdf">maccosmeticswholesaleoutlet hello kitty series c 8</a> <a href="http://hotelolecko.pl/upload/maccosmeticswholesalers_mac_makeup_10pcs_brushes_c_1.pdf">maccosmeticswholesalers mac makeup 10pcs brushes c 1</a> <a href="http://hdlacewigs.co.uk/upload/maccosmeticswholesalers_mac_false_eyelashes_c_3.pdf">maccosmeticswholesalers mac false eyelashes c 3</a> <a href="http://chelseabikes.co.uk/upload/maccosmeticswholesaleoutlet_mac_cosmetic_lipgloss_c.pdf">maccosmeticswholesaleoutlet mac cosmetic lipgloss c</a> <a href="http://paulinhodrumimoveis.com.br/upload/maccosmeticswholesalers_mac_eyeliner_gel_c_29.pdf">maccosmeticswholesalers mac eyeliner gel c 29</a>

  8. fiftyshadesofgreyonlinemovie.com on 02/14/2016 7:27 p.m. #

    Zinnia's Weblog - 神奇的位运算

  9. Www.Tuugo.us on 02/15/2016 12:02 a.m. #

    Zinnia's Weblog - 神奇的位运算

  10. Oldtvshowondvd.com on 02/15/2016 12:02 a.m. #

    Zinnia's Weblog - 神奇的位运算

  11. maccosmeticswholesalers mac makeup bag c 13 on 02/15/2016 1:21 a.m. #

    <a href="http://www.caveexplorer.eu/upload/maccosmeticswholesalers_mac_makeup_32pcs_brushes_c_1.pdf">maccosmeticswholesalers mac makeup 32pcs brushes c 1</a> <a href="http://www.therightgear.co.uk/upload/maccosmeticswholesalers_mac_makeup_pencil_c_17.pdf">maccosmeticswholesalers mac makeup pencil c 17</a> <a href="http://www.zoobeez.com.au/upload/maccosmeticswholesalers_mac_powder_foundation_c_35.pdf">maccosmeticswholesalers mac powder foundation c 35</a> <a href="http://www.islandmade.co.uk/upload/maccosmeticswholesaleoutlet_mac_makeup_13pcs_brushes.pdf">maccosmeticswholesaleoutlet mac makeup 13pcs brushes</a> <a href="http://radio-volna.ru/upload/maccosmeticswholesaleoutlet_mac_makeup_16pcs_brushes.pdf">maccosmeticswholesaleoutlet mac makeup 16pcs brushes</a> <a href="http://caroleridley.com/upload/maccosmeticswholesaleoutlet_mac_cosmetics_eyeshadow.pdf">maccosmeticswholesaleoutlet mac cosmetics eyeshadow</a>

  12. www.avvo.com on 02/15/2016 4:34 a.m. #

    Zinnia's Weblog - 神奇的位运算

  13. peruvian virgin hair on 02/16/2016 4:57 a.m. #

    carry peruvian virgin hair with everything else. Normally nice style. Would like to see peruvian virgin hair are available Coffee brown leafy.

  14. issuu.com on 02/16/2016 5:15 a.m. #

    Zinnia's Weblog - 神奇的位运算

  15. dresses.qy119.com on 02/17/2016 7:07 p.m. #

    Zinnia's Weblog - 神奇的位运算

  16. cheap Herve Leger 2013 pre fall on 02/17/2016 8:09 p.m. #

    They are also a good source of dietary fiber, riboflavin, phosphorus, magnesium and potassium.

  17. arthur jones jersey on 02/17/2016 8:40 p.m. #

    This video post Zinnia's Weblog - 神奇的位运算 is truly great, the echo quality and the picture quality of this video post is actually remarkable.

  18. teledildonics dog toys for guys on 02/18/2016 11:26 a.m. #

    Zinnia's Weblog - 神奇的位运算

  19. aabacosmallbusiness.optimizelocation.com on 02/18/2016 5:28 p.m. #

    Zinnia's Weblog - 神奇的位运算

  20. www.realmadridjerseyshop.com on 02/18/2016 5:28 p.m. #

    Zinnia's Weblog - 神奇的位运算

  21. jual hand pallet full electric on 02/18/2016 8:55 p.m. #

    Zinnia's Weblog - 神奇的位运算

  22. obat pelangsing badan on 02/19/2016 1:39 p.m. #

    Zinnia's Weblog - 神奇的位运算

  23. Free Offline Games Windows 8 on 02/19/2016 10:26 p.m. #

    Zinnia's Weblog - 神奇的位运算

  24. best beer on 02/20/2016 2:27 a.m. #

    Zinnia's Weblog - 神奇的位运算

  25. boneka sex on 02/20/2016 3:37 a.m. #

    Zinnia's Weblog - 神奇的位运算

  26. oakley radarlock path sale on 02/20/2016 10:34 a.m. #

    Ahaa, its fastidious dialogue on the topic of this Zinnia's Weblog - 神奇的位运算 at this place at this blog, I have read all that, so at this time me also commenting here.

  27. The.Advanced.Grammar.Book.Second.Edition on 02/20/2016 5:02 p.m. #

    Zinnia's Weblog - 神奇的位运算

  28. www.youtube.com on 02/21/2016 2:16 p.m. #

    Zinnia's Weblog - 神奇的位运算

  29. booking hotel on 02/21/2016 7:59 p.m. #

    Zinnia's Weblog - 神奇的位运算

  30. cs go for sale on 02/25/2016 8:28 p.m. #

    <a href="http://www.doori-nyc.com/uploads/cs-go-skins-quality.html">cs go skins quality</a>

  31. drive.google.com on 02/26/2016 5:33 a.m. #

    Zinnia's Weblog - 神奇的位运算

  32. cs go for pc on 02/27/2016 2:36 a.m. #

    <a href="http://www.socorroswcd.com/uploads/shop-cs-go.html">shop cs go</a>

  33. butterfly sale on 02/27/2016 8:42 a.m. #

    Zinnia's Weblog - 神奇的位运算 Piece of writing writing is also a fun, if you be familiar with afterward you can write otherwise it is complex to write.

  34. cs global offensive on 02/27/2016 10:47 a.m. #

    <a href="http://www.anglingthailand.com/uploads/the-italy-collection-cs-go.html">the italy collection cs go</a>

  35. ipage discount promotions on 02/27/2016 9:33 p.m. #

    Zinnia's Weblog - 神奇的位运算

  36. cheap air max on 02/27/2016 11:23 p.m. #

    Zinnia's Weblog - 神奇的位运算,cheap air max

  37. csgo key on 02/28/2016 4:41 a.m. #

    <a href="http://www.fuscoauctions.com/uploads/cs-go-games.html">cs go games</a>

  38. csgo database on 02/28/2016 5:05 a.m. #

    <a href="http://www.winegrapebroker.com/uploads/buy-counter-strike-global-offensive.html">buy counter strike global offensive</a>

  39. maccosmeticswholesalers mac makeup lip balm c 38 on 02/28/2016 5:48 a.m. #

    <a href="http://urodawdomu.pl/upload/maccosmeticswholesalers_912_color_eyeshadow_c_18_31.pdf">maccosmeticswholesalers 912 color eyeshadow c 18 31</a> <a href="http://btfls.com/upload/maccosmeticswholesaleoutlet_bobbi_brown_brushes_c_37.pdf">maccosmeticswholesaleoutlet bobbi brown brushes c 37</a> <a href="http://sneg925.ru/upload/maccosmeticswholesalers_mac_makeup_8pcs_brushes_c_10.pdf">maccosmeticswholesalers mac makeup 8pcs brushes c 10</a> <a href="http://g5k.net/upload/maccosmeticswholesalers_mac_makeup_bag_c_13.pdf">maccosmeticswholesalers mac makeup bag c 13</a> <a href="http://kportersports.com/upload/maccosmeticswholesaleoutlet_lady_gaga_collection_c_1.pdf">maccosmeticswholesaleoutlet lady gaga collection c 1</a> <a href="http://rhemed.com/upload/maccosmeticswholesaleoutlet_lady_gaga_collection_c_1.pdf">maccosmeticswholesaleoutlet lady gaga collection c 1</a>

  40. cs go skin market on 02/29/2016 12:35 p.m. #

    <a href="http://www.greenwoodscharter.org/uploads/cs-go-store.html">cs go store</a>

  41. cs go competitive servers on 02/29/2016 1:14 p.m. #

    <a href="http://nteu45.org/uploads/cs-go-player.html">cs go player</a>

  42. esl csgo plugin on 03/01/2016 5:29 p.m. #

    <a href="http://www.vanishingveins.net/news/new-gun-csgo.html">new gun csgo</a>

  43. yuantuo on 03/02/2016 9:52 a.m. #

    http://rekrutacja.lekkitornister.org/mm-jp-0Z55.html香水 カルバンクライン

  44. cs go stickers on 03/03/2016 3:03 p.m. #

    <a href="http://www.thejuliafoundation.com/news/csgo-community.html">csgo community</a>

  45. counter strike global offensive case key on 03/03/2016 7:52 p.m. #

    <a href="http://www.davekaplan.com/news/cs-go-skin-price-list.html">cs go skin price list</a>

  46. compare insurance quotes on 03/04/2016 7:37 p.m. #

    Zinnia's Weblog - 神奇的位运算

  47. women's waterproof shoes on 03/08/2016 5:45 p.m. #

    Make the most of this valuable women's waterproof shoes each time Since my lifestyle possibly be little your feet throughout the working day due to the fact I realize at the conclusion of from my legs it is during great good condition!

  48. nike air max ltd 2 45 5 on 03/13/2016 1:03 p.m. #

    Hey there, You've done an incredible job. I'll definitely digg it and personally suggest to my friends. I'm sure they wil be benefited from this site.

  49. solde longchamp 2010 on 03/13/2016 3:50 p.m. #

    I know this web page provides quality based articles and other data, is there any other web site which offers such information in quality?

  50. primarkonlineshop on 03/14/2016 8:15 a.m. #

    The New York Mets are willing to do most anything to get David Wright elected to the All Star team. Well, almost anything.

  51. pearce engineering on 03/14/2016 8:45 p.m. #

    Zinnia's Weblog - 神奇的位运算

  52. sitedchaussure on 03/17/2016 3:55 p.m. #

    "The side effects are not as bad." Fantry added. "We can treat people without harming the rest of the body."

  53. financing gym equipment on 03/19/2016 5:01 a.m. #

    Zinnia's Weblog - 神奇的位运算

  54. investment manager on 03/30/2016 9:38 p.m. #

    Zinnia's Weblog - 神奇的位运算

  55. boy names for cats on 04/01/2016 10:54 p.m. #

    Zinnia's Weblog - 神奇的位运算

  56. รถรับจ้างขนของ on 04/02/2016 7:28 p.m. #

    Zinnia's Weblog - 神奇的位运算

  57. system traders on 04/04/2016 11:28 a.m. #

    Zinnia's Weblog - 神奇的位运算

  58. jual genset shark on 04/05/2016 2:25 a.m. #

    Zinnia's Weblog - 神奇的位运算

  59. site on 04/10/2016 5:37 a.m. #

    Zinnia's Weblog - 神奇的位运算

  60. pittsburgh retaininig wall builders on 04/11/2016 2 p.m. #

    Zinnia's Weblog - 神奇的位运算

  61. nie mehr akne ebook on 04/16/2016 12:50 p.m. #

    Zinnia's Weblog - 神奇的位运算

  62. mulberry glasgow on 04/19/2016 4:59 p.m. #

    "I think somebody did something that they weren't supposed to do," he continued, referring to Tom Brady. "I'm not sure who they're blaming touched an air needle, but I'm sure they had their way of instructing the way things need to be done, because that's how they like their balls."

  63. web site on 04/23/2016 2:07 a.m. #

    Zinnia's Weblog - 神奇的位运算

  64. w mosquito net on 04/24/2016 1:48 p.m. #

    Zinnia's Weblog - 神奇的位运算

  65. sac plume hermes on 04/25/2016 10:09 a.m. #

    "She said it was intimidating not being able to see if it was a man or a woman," says Qureshi, a first year recreation therapy student.

  66. Payday loan online on 05/02/2016 5:37 p.m. #

    Zinnia's Weblog - 神奇的位运算

  67. mind map on 05/09/2016 1:56 p.m. #

    Zinnia's Weblog - 神奇的位运算

  68. bad credit payday on 05/13/2016 4:01 p.m. #

    Zinnia's Weblog - 神奇的位运算

  69. zip code on 05/27/2016 3:45 p.m. #

    Zinnia's Weblog - 神奇的位运算

  70. birkenstock sandaler forhandler on 05/30/2016 11:39 a.m. #

    andunskilfully relateret. Distant begivenheder må have været blandet sammen, ogKommissionens handlinger én mand givet til en anden. Disse er dog aredeficiencies i historien, som intet menneske er nu være forbudt. Det wereenough, hvis det, der er endnu mulighed for at undersøge blev accuratelyinspected og retfærdigt repræsenteret; men sådan er slaphed af Highlandconversation, at Inquirer er holdt i kontinuerlig spænding, og ved en Kinda REJSE TIL DEN VESTLIGE øer SCOTLAND55of intellektuel retrogradation, ved mindre som han hører more.In øerne den plaid er sjældent slidte. Den lov, hvorved theHighlanders er blevet nødt til at skifte form af deres påklædning, har, inall de steder, vi har besøgt, er blevet universelt adlød. Jeg har seenonly én Herre helt klædt i den gamle vane, og ved

Trackback URL

Comments

Comments are closed.