成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LintCode] Swap Bits

ls0609 / 3346人閱讀

Problem

Write a program to swap odd and even bits in an integer with as few instructions as possible (e.g., bit 0 and bit 1 are swapped, bit 2 and bit 3 are swapped, and so on).

Example

5 = (101)2 => (1010)2 = 10

Solution
public class Solution {
    /*
     * @param x: An integer
     * @return: An integer
     */
    public int swapOddEvenBits(int x) {
        //keep all odd "1"
        int odd = x & 0x55555555;
        //keep all even "1"
        int even = x & 0xaaaaaaaa;
        //shift odd "1"s left
        odd <<= 1;
        //shift even "1"s right (unsigned)
        even >>>= 1;
        return odd + even;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/68191.html

相關(guān)文章

  • [LintCode/CC] Update Bits [Merge Bits]

    Problem Given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set all bits between i and j in N equal to M (e g , M becomes a substring of N located at i and starting at...

    KnewOne 評論0 收藏0
  • [LintCode] Flip Bits

    摘要:的二進制補碼就是個,因此這道題一定要考慮正負(fù)號的問題。然后去檢查的二進制包含多少個,方法是對的每一位除以取余。如果為,就說明那一位為,即和在那一位不同,需要進行轉(zhuǎn)換。每次取余之后,減小為二分之一,刪除已經(jīng)檢查過的高位。 Problem Determine the number of bits required to flip if you want to convert integer...

    joyvw 評論0 收藏0
  • [LintCode] UTF-8 Validation

    Problem A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules: For 1-byte character, the first bit is a 0, followed by its unicode code.For n-bytes character, the first n...

    tolerious 評論0 收藏0
  • [LintCode] Swap Two Nodes in Linked List

    摘要:建立結(jié)點,指向可能要對進行操作。找到值為和的結(jié)點設(shè)為,的前結(jié)點若和其中之一為,則和其中之一也一定為,返回頭結(jié)點即可。正式建立,,以及對應(yīng)的結(jié)點,,然后先分析和是相鄰結(jié)點的兩種情況是的前結(jié)點,或是的前結(jié)點再分析非相鄰結(jié)點的一般情況。 Problem Given a linked list and two values v1 and v2. Swap the two nodes in th...

    wua_wua2012 評論0 收藏0
  • [LintCode] Swap Nodes in Pairs

    摘要:指針為,我們選擇的兩個結(jié)點是和。要注意循環(huán)的邊界條件,這兩個結(jié)點不能為空。主要思路是先用和兩個新結(jié)點去保存和兩個結(jié)點。完成交換之后,連接和,并讓前進至此時的結(jié)點。 Problem Given a linked list, swap every two adjacent nodes and return its head. Example Given 1->2->3->4, you sh...

    EscapedDog 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<