当前位置:   article > 正文

Java | Leetcode Java题解之第190题颠倒二进制位

Java | Leetcode Java题解之第190题颠倒二进制位

题目:

题解:

  1. public class Solution {
  2. private static final int M1 = 0x55555555; // 01010101010101010101010101010101
  3. private static final int M2 = 0x33333333; // 00110011001100110011001100110011
  4. private static final int M4 = 0x0f0f0f0f; // 00001111000011110000111100001111
  5. private static final int M8 = 0x00ff00ff; // 00000000111111110000000011111111
  6. public int reverseBits(int n) {
  7. n = n >>> 1 & M1 | (n & M1) << 1;
  8. n = n >>> 2 & M2 | (n & M2) << 2;
  9. n = n >>> 4 & M4 | (n & M4) << 4;
  10. n = n >>> 8 & M8 | (n & M8) << 8;
  11. return n >>> 16 | n << 16;
  12. }
  13. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/773372
推荐阅读
相关标签
  

闽ICP备14008679号