[小米]懂二進位

來源:互聯網
上載者:User

標籤:exp   class   限制   lin   小米   href   思路   題目   ntb   

時間限制:1秒 空間限制:32768K 熱度指數:35899 演算法知識視頻講解 題目描述世界上有10種人,一種懂二進位,一種不懂。那麼你知道兩個int32整數m和n的二進位表達,有多少個位(bit)不同嗎?
輸入例子:
1999 2299

輸出例子:
7
思路:在二進位中,統計兩個數的相應位不同可以利用異或,運算結果相同為0,不同為1,所以異或結果中1的個數就是不同的位元。
 1 class Solution { 2 public: 3     /** 4      * 獲得兩個整形二進位表達位元不同的數量 5      *  6      * @param m 整數m 7      * @param n 整數n 8      * @return 整型 9      */10     int countBitDiff(int m, int n) {11         int res=m^n;12         int num=0;13         14         while(res)15             {16                 if((res&1)==1)17                     ++num;18                 res=res>>1;19         }20         21         return num;22 23     }24 };

 

[小米]懂二進位

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.