开发者

Bit-wise operation tricks [duplicate]

This question already has answers here: 开发者_Go百科 Closed 11 years ago.

Possible Duplicate:

What USEFUL bitwise operator code tricks should a developer know about?

Hi,

What are some neat tricks with using bit-wise operations. I know that unless you're programming in C you won't have much encounters with operating on bit level. Nonetheless, there are some neat tricks that you can apply in even higher level languages. Here are a few that I already know.

  1. bit mask: Can hold a collection of boolean values
  2. XOR Swap: Swap 2 values in place without a third variable
  3. XOR Linked List: Create a doubly linked list with each node only hold one address value

What are some others?


find whether a number is odd or not

(number & 1)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜