rijyakurinn
rijyakurinn
采纳率100%
2019-07-10 09:30 阅读 353
已采纳

Java移位运算中,先移位再&运算和先&再移位有什么区别吗?

方法是翻转二进制位,

  • 输入00000010100101000001111010011100
  • 输出00111001011110000010100101000000
 public int reverseBits(int n) {
        int res = 0;
        int count = 0;
        while(count<32) {
            res = res << 1;
            res += n&1;
            n >>>=1;
            count++;
        }
        return res;
    }

先移位再进行&运算结果就是正确的,但是如果先进行&,再进行移位 结果是多向左 移动了一位,请问是为什么啊....

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    JKL852qaz 林兴洋 2019-07-10 22:15

    先移位再进行&运算结果就是正确的,但是如果先进行移位,再进行&

    你这描述不是一个意思么。你是想说 先位移再进行& 和先&在进行位移?

    -----------------------------------------分割线

    写了代码看了一下,是多移动了一位,本来应该移动32次的,但是由于第一个位置的可以不用移动,直接赋值,可以少移动一次。

    所以先位移的话,就先消耗了一次移动(第一次移动),总移动31次
    先&的话,总移动32次

    public class Test {
    
      public static int reversalMoveOneMoreBit(int nValue) {
        int nDst = 0;
        int nLength = 32;
        System.out.println("reversalMoveOneMoreBit process:");
        for (int i = 0; i < nLength; i++) {
          nDst = ((nValue >> i) & 0X01) == 1 ? (nDst | 0X01) : (nDst | 0X00);
          System.out.print("before:");
          printfBinary(nDst);
          nDst = (nDst << 1);
          System.out.print("after :");
          printfBinary(nDst);
        }
        return nDst;
      }
    
      public static int reversal(int nValue) {
        int nDst = 0;
        int nLength = 32;
        System.out.println("reversal process:");
        for (int i = 0; i < nLength; i++) {
          System.out.print("before:");
          printfBinary(nDst);
          nDst = (nDst << 1);
          System.out.print("after :");
          printfBinary(nDst);
          nDst = ((nValue >> i) & 0X01) == 1 ? (nDst | 0X01) : (nDst | 0X00);
        }
        return nDst;
      }
    
      public static void printfBinary(int nValue) {
        int nIndex = 0;
        int nLength = 32;
        int naBinary[] = new int[nLength];
        for (int i = 0; i < nLength; i++) {
          nIndex = (nLength - i - 1);
          naBinary[nIndex] = (nValue >> i) & 0X01;
        }
        for (int i = 0; i < nLength; i++) { 
          boolean bPrintSpace = ((0 != i) && (i % 8 == 0));
          if (bPrintSpace) {
            System.out.print(" ");
          }
          System.out.print(naBinary[i]);
        }
        System.out.println();
      }
    
      public static void main(String args[]) {
        //                         high <--> low
        // String sSrc = "00000010 10010100 00011110 10011100";
        // String sDst = "00111001 01111000 00101001 01000000"
        int nSrc = 0X00FF00FF;
        int nReversalDst = reversal(nSrc);
        int nReversalMoveOneMoreBitDst = reversalMoveOneMoreBit(nSrc);
    
        System.out.println(nSrc + " binary:");
        printfBinary(nSrc);
        System.out.println(nReversalDst + " binary:");
        printfBinary(nReversalDst);
        System.out.println(nReversalMoveOneMoreBitDst + " binary:");
        printfBinary(nReversalMoveOneMoreBitDst);
      }
    }
    

    结果

    reversal process:
    before:00000000 00000000 00000000 00000000
    after :00000000 00000000 00000000 00000000
    before:00000000 00000000 00000000 00000001
    after :00000000 00000000 00000000 00000010
    before:00000000 00000000 00000000 00000011
    after :00000000 00000000 00000000 00000110
    before:00000000 00000000 00000000 00000111
    after :00000000 00000000 00000000 00001110
    before:00000000 00000000 00000000 00001111
    after :00000000 00000000 00000000 00011110
    before:00000000 00000000 00000000 00011111
    after :00000000 00000000 00000000 00111110
    before:00000000 00000000 00000000 00111111
    after :00000000 00000000 00000000 01111110
    before:00000000 00000000 00000000 01111111
    after :00000000 00000000 00000000 11111110
    before:00000000 00000000 00000000 11111111
    after :00000000 00000000 00000001 11111110
    before:00000000 00000000 00000001 11111110
    after :00000000 00000000 00000011 11111100
    before:00000000 00000000 00000011 11111100
    after :00000000 00000000 00000111 11111000
    before:00000000 00000000 00000111 11111000
    after :00000000 00000000 00001111 11110000
    before:00000000 00000000 00001111 11110000
    after :00000000 00000000 00011111 11100000
    before:00000000 00000000 00011111 11100000
    after :00000000 00000000 00111111 11000000
    before:00000000 00000000 00111111 11000000
    after :00000000 00000000 01111111 10000000
    before:00000000 00000000 01111111 10000000
    after :00000000 00000000 11111111 00000000
    before:00000000 00000000 11111111 00000000
    after :00000000 00000001 11111110 00000000
    before:00000000 00000001 11111110 00000001
    after :00000000 00000011 11111100 00000010
    before:00000000 00000011 11111100 00000011
    after :00000000 00000111 11111000 00000110
    before:00000000 00000111 11111000 00000111
    after :00000000 00001111 11110000 00001110
    before:00000000 00001111 11110000 00001111
    after :00000000 00011111 11100000 00011110
    before:00000000 00011111 11100000 00011111
    after :00000000 00111111 11000000 00111110
    before:00000000 00111111 11000000 00111111
    after :00000000 01111111 10000000 01111110
    before:00000000 01111111 10000000 01111111
    after :00000000 11111111 00000000 11111110
    before:00000000 11111111 00000000 11111111
    after :00000001 11111110 00000001 11111110
    before:00000001 11111110 00000001 11111110
    after :00000011 11111100 00000011 11111100
    before:00000011 11111100 00000011 11111100
    after :00000111 11111000 00000111 11111000
    before:00000111 11111000 00000111 11111000
    after :00001111 11110000 00001111 11110000
    before:00001111 11110000 00001111 11110000
    after :00011111 11100000 00011111 11100000
    before:00011111 11100000 00011111 11100000
    after :00111111 11000000 00111111 11000000
    before:00111111 11000000 00111111 11000000
    after :01111111 10000000 01111111 10000000
    before:01111111 10000000 01111111 10000000
    after :11111111 00000000 11111111 00000000
    reversalMoveOneMoreBit process:
    before:00000000 00000000 00000000 00000001
    after :00000000 00000000 00000000 00000010
    before:00000000 00000000 00000000 00000011
    after :00000000 00000000 00000000 00000110
    before:00000000 00000000 00000000 00000111
    after :00000000 00000000 00000000 00001110
    before:00000000 00000000 00000000 00001111
    after :00000000 00000000 00000000 00011110
    before:00000000 00000000 00000000 00011111
    after :00000000 00000000 00000000 00111110
    before:00000000 00000000 00000000 00111111
    after :00000000 00000000 00000000 01111110
    before:00000000 00000000 00000000 01111111
    after :00000000 00000000 00000000 11111110
    before:00000000 00000000 00000000 11111111
    after :00000000 00000000 00000001 11111110
    before:00000000 00000000 00000001 11111110
    after :00000000 00000000 00000011 11111100
    before:00000000 00000000 00000011 11111100
    after :00000000 00000000 00000111 11111000
    before:00000000 00000000 00000111 11111000
    after :00000000 00000000 00001111 11110000
    before:00000000 00000000 00001111 11110000
    after :00000000 00000000 00011111 11100000
    before:00000000 00000000 00011111 11100000
    after :00000000 00000000 00111111 11000000
    before:00000000 00000000 00111111 11000000
    after :00000000 00000000 01111111 10000000
    before:00000000 00000000 01111111 10000000
    after :00000000 00000000 11111111 00000000
    before:00000000 00000000 11111111 00000000
    after :00000000 00000001 11111110 00000000
    before:00000000 00000001 11111110 00000001
    after :00000000 00000011 11111100 00000010
    before:00000000 00000011 11111100 00000011
    after :00000000 00000111 11111000 00000110
    before:00000000 00000111 11111000 00000111
    after :00000000 00001111 11110000 00001110
    before:00000000 00001111 11110000 00001111
    after :00000000 00011111 11100000 00011110
    before:00000000 00011111 11100000 00011111
    after :00000000 00111111 11000000 00111110
    before:00000000 00111111 11000000 00111111
    after :00000000 01111111 10000000 01111110
    before:00000000 01111111 10000000 01111111
    after :00000000 11111111 00000000 11111110
    before:00000000 11111111 00000000 11111111
    after :00000001 11111110 00000001 11111110
    before:00000001 11111110 00000001 11111110
    after :00000011 11111100 00000011 11111100
    before:00000011 11111100 00000011 11111100
    after :00000111 11111000 00000111 11111000
    before:00000111 11111000 00000111 11111000
    after :00001111 11110000 00001111 11110000
    before:00001111 11110000 00001111 11110000
    after :00011111 11100000 00011111 11100000
    before:00011111 11100000 00011111 11100000
    after :00111111 11000000 00111111 11000000
    before:00111111 11000000 00111111 11000000
    after :01111111 10000000 01111111 10000000
    before:01111111 10000000 01111111 10000000
    after :11111111 00000000 11111111 00000000
    before:11111111 00000000 11111111 00000000
    after :11111110 00000001 11111110 00000000
    16711935 binary:
    00000000 11111111 00000000 11111111
    -16711936 binary:
    11111111 00000000 11111111 00000000
    -33423872 binary:
    11111110 00000001 11111110 00000000
    
    点赞 评论 复制链接分享

相关推荐