Algorithm_swap odd_even bit in an interger

来源:互联网 发布:seo交流中心 编辑:程序博客网 时间:2024/06/11 15:28

Write a program to swap odd and even bits in an integer with as few instructions as possible

 (e.g., bit 0 and bit1 are swapped, bit 2 and bit 3 are swapped, and so on).


方法:

先取偶数位,然后往左移1位,

再取奇数位,然后往右移动1位。

最后将得到的数,进行或运算。


时间复杂度:

O(1)

空间复杂度:

O(1)

0 0