【问题标题】:Use java to perform binary shifts [closed]使用java执行二进制移位[关闭]
【发布时间】:2013-09-15 03:43:40
【问题描述】:

我必须在 java 中获取二进制数据并对其执行移位。 例如11110000 当我将其右移 2 时,即 11110000>>00111100

问题是,我不知道如何在 Java 中存储这样的数据,因为字节类型会将其转换为十进制格式。我需要使用位置 6 的位并将其与其他值进行异或。

我只需要知道如何实现存储二进制数据和执行所需班次的能力。

【问题讨论】:

  • 谷歌它有成千上万的指南/教程
  • 只需使用ints 或longs 或其他一些数字数据类型。该数字仍将以 10 为基数,但数字在内部以 2 为基数,因此位操作将适用于这些数据类型。
  • 就这么做吧。你会看到结果。它会以适合其类型的形式出现,但它为什么重要?无论如何都会发生转变。当你尝试过时,你可以带着问题回到这里,显示你的代码和结果。否则,您只会收集反对票。
  • “字节类型将其转换为十进制格式”这句话毫无意义。 byte 是一个没有格式的数字;它只是你如何读入或读出一个数字,使它成为二进制或十进制或其他。
  • [关于编辑] 反正问题已经结束了,你就这样吧。如果你真的需要删除问题,那么你应该标记它,选择other并向版主解释你为什么要删除它。

标签: java binary xor shift


【解决方案1】:

如果您使用的是整数,它将以数字格式显示,您可以使用Integer.toBinaryString(yourByte) 解决它

其次,如果您使用整数来存储格式为1... 的二进制数(最左边的位是1),那么使用操作>> 会将数字右移但输入“新”@ 987654324@ 到最左边的位。在这种情况下你想要做的实际上是使用 >>> 来防止这种情况发生。

如果您使用int 来存储字节,并且想要开始进行各种位操作,则必须使用mask,例如,在第 3 位和第 5 位之间切换位:

    int number = 7; //just an example, here binary rep: 00111 => requested output will be 10011
    System.out.println("number = " + Integer.toBinaryString(number));
    int mask3Bit = 4;//binary rep: 0100
    System.out.println("mask3Bit = " + Integer.toBinaryString(mask3Bit));

    int mask5Bit = 16; //binary rep: 10000
    System.out.println("mask5Bit = " + Integer.toBinaryString(mask5Bit));

    // now we'll create a mask that has all the bits on except the 3rd and 5th bit:
    int oppositeMask = -1;
    oppositeMask ^= mask3Bit;
    oppositeMask ^= mask5Bit;
    System.out.println("oppositeMask = " + Integer.toBinaryString(oppositeMask));

    //check if the 3rd bit is on:
    mask3Bit = number & mask3Bit;
    //shift twice to the right
    mask3Bit <<= 2;
    System.out.println("mask3Bit = " + Integer.toBinaryString(mask3Bit));
    //now do the same with the 5th bit
    //check if the 5th bit is on:
    mask5Bit = number & mask5Bit;
    //shift twice to the right
    mask5Bit >>= 2;
    System.out.println("mask5Bit = " + Integer.toBinaryString(mask5Bit));

    //now we'll turn off the 3rd and 5th bits in the original number
    number &= oppositeMask;
    System.out.println("number = " + Integer.toBinaryString(number));
    //and use the masks to switch the bits
    number |= mask3Bit;
    number |= mask5Bit;
    //let's check it out now:
    System.out.println("new number = " + Integer.toBinaryString(number));

输出:

number = 111
mask3Bit = 100
mask5Bit = 10000
oppositeMask = 11111111111111111111111111101011
mask3Bit = 10000
mask5Bit = 0 //here it's zero cause the original number has zero in the 5th bit and we used &. If the bit was on we would have gotten '100'
number = 11
new number = 10011

【讨论】:

  • 字节将其转换为十进制格式,我想使用特定位置的位来执行所需的操作。
  • @chettyharish 没有看到你的代码,很难提供比我已经做过的更多的信息 ;)
  • 我没有代码,因为我不知道从哪里开始。我必须编写三个不断移位的二进制数据,并且移位取决于特定的位值。比如说,如果第一个二进制数据在第 7 个位置有 1,那么它会移动两次。
  • 我原来的评论仍然有效...
  • 我试过 redFive,但大多数都只是谈论移位运算符。在我的一个步骤中,我还必须更改一个特定的位。 X5=X1 XOR X2 XOR X3
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-25
  • 1970-01-01
  • 2021-09-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多