在32位整数上进行RotateLeft操作

时间:2017-03-28 03:45:16

标签: java rotation bit-manipulation bit

我一直在为我的计算机组织课程开发不同的项目,我们一直致力于BitWise操作。我们当前的任务是为java编写一个自制的'rotateLeft'方法。

当java已经启用时,通过使用Integer.rotateLeft,我当前的任务是编写一个来处理这个程序。

注意:int变量等于这些位串

x1 = 3 = 00000000000000000000000000000011
x2 = -11 = 1111111111111111111111111111110101

我目前的计划是:

public class Demo
 {
     public static void main(String[]args)
    {
         int x1=3, x2=-11;
         System.out.print("x1:            ");
         BitWise.printbit(x1);
         System.out.print("rotateLeft(x1,2):  ");
         BitWise.printbit(rotateLeft(x1,2));
         System.out.print("x2:            ");
         BitWise.printbit(x2);
         System.out.print("rotateLeft(x2,2):  ");
          BitWise.printbit(rotateLeft(x2,2));
        }
         public static int rotateLeft(int i, int distance)
         {
           int mask= i>>distance;
          return mask;

          }
       }

此操作适用于x1位模式,但是,它只是移位,而不是实际旋转它们。

有什么建议吗?

2 个答案:

答案 0 :(得分:2)

这对我有用:

public static void main(String[] args) {
  int x1 = 3;
  int x2 = -11;

  int x1IntegerRotated = Integer.rotateLeft(x1, 2);
  int x1SelfRotated = rotateLeft(x1, 2);
  System.out.printf("x1 = %d(%s)%n",               x1,               printIntBitwise(x1));
  System.out.printf("x1IntegerRotated = %d(%s)%n", x1IntegerRotated, printIntBitwise(x1IntegerRotated));
  System.out.printf("x1SelfRotated = %d(%s)%n",    x1SelfRotated,    printIntBitwise(x1SelfRotated));

  System.out.println();

  int x2IntegerRotated = Integer.rotateLeft(x2, 2);
  int x2SelfRotated = rotateLeft(x2, 2);
  System.out.printf("x2 = %d(%s)%n",               x2,               printIntBitwise(x2));
  System.out.printf("x2IntegerRotated = %d(%s)%n", x2IntegerRotated, printIntBitwise(x2IntegerRotated));
  System.out.printf("x2SelfRotated = %d(%s)%n",    x2SelfRotated,    printIntBitwise(x2SelfRotated));
}

private static int rotateLeft(int value, int distance) {
  int mask = (1 << distance) - 1;
  int leftPart = (value << distance) & (~mask);
  int rightPart = (value >> (32 - distance)) & (mask);

  int result = leftPart | rightPart;

  return result;
}

private static String printIntBitwise(int a) {
  StringBuilder sb = new StringBuilder();

  for(int i = 1; i <= 32; i++) {
    sb.append(Math.abs((a & (1 << (32 - i))) >> (32 - i)));
  }

  return sb.toString();
}

答案 1 :(得分:1)

好的,我已经找到了一种方法:

//This is a helper function; it returns an int where the leftmost num bits are 1 and the rest are 0
static int get1s(int num) {
    int buf = 0;
    for (int i = 31; i>31-num;i--) {
        buf += 1 << i;
    }
    return buf;
}

static int rotateLeft(int i, int distance) {
    int end = i & get1s(distance);
    int mov = end >>> 32 - distance;
    int shift = i << distance;
    return shift + mov;
}

基本上,它的工作方式是逐行的,如下所示:

  • end设置为最左边的distance位,而不实际移动它。

  • 设置mov等于end移位到足以使其成为最右边的distance位。

  • shift设置为等于移位值。

  • 返回shiftmov,从而将最左边的位置放在右边。

如果您想更详细地了解其工作原理,可以在每个步骤后打印结果:

static int rotateLeftWithPrint(int i, int distance) {
    int end = i & get1s(distance);
    System.out.println(Integer.toBinaryString(end));
    int mov = end >>> 32 - distance;
    System.out.println(Integer.toBinaryString(mov));
    int shift = i << distance;
    System.out.println(Integer.toBinaryString(shift));

    System.out.println(Integer.toBinaryString(shift+mov));
    return shift + mov;
}

注意Integer.toBinaryString 显示前导零,这就是为什么它不会一直打印{{1}同等长度的.s。

编辑 - 认为这可能有用,您可以通过在数字前放置String来添加二进制文字; 0b相当于3