[英]RotateLeft operation on a 32 bit Integer
我正在為我的計算機組織課程從事不同的項目,並且一直在從事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位模式,但是,它僅移位位,而不實際旋轉位。
有什么建議么?
這對我有用:
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();
}
好的,我想出了一種方法:
//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
設置為等於偏移值。
返回shift
加mov
,從而將曾經是最左邊的位放在右邊。
如果要詳細了解其工作原理,可以在每個步驟之后打印結果:
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
沒有顯示前導零,所以這就是為什么它不會總是打印String
S也是一樣的長度。
編輯 -認為這可能有用,您可以通過在數字前放置0b
來添加二進制文字; 3
等效於0b00000000000000000000000000000011
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.