Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java执行二进制移位_Java_Binary_Xor_Shift - Fatal编程技术网

使用java执行二进制移位

使用java执行二进制移位,java,binary,xor,shift,Java,Binary,Xor,Shift,我必须在java中获取二进制数据并对其执行移位。 例如,当我将其右移2时为11110000,即11110000>>00111100 问题是,我不知道如何在Java中存储这样的数据,因为字节类型将其转换为十进制格式。我需要在位置6使用位,并将其与其他值异或 我只需要知道如何实现存储二进制数据和执行所需移位的功能。如果使用整数,它将以数字格式显示,您可以使用integer.toBinaryString(yourByte) 其次,如果使用整数以1…(最左边的位是1)的格式存储二进制数,则使用操作>将数

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

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


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

如果使用整数,它将以数字格式显示,您可以使用
integer.toBinaryString(yourByte)

其次,如果使用整数以
1…
(最左边的位是1)的格式存储二进制数,则使用操作
>
将数字向右移动,但在最左边的位中输入一个“新的”
1
。在这种情况下,您要做的是实际使用
>>
,这可以防止出现这种情况

如果您使用
int
存储字节,并且希望开始执行各种位操作,则必须使用
掩码
,例如,在第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));

Google it man有成千上万的指南/教程,只需使用
int
s或
long
s或其他数字数据类型即可。数字仍将以10为基数显示,但数字在内部以2为基数,因此位操作将适用于这些数据类型。你会看到结果的。对于它的类型,它将以适当的形式出现,但是为什么它很重要呢?无论如何,这种转变都会发生。当你尝试的时候,你可以带着问题回到这里,展示你的代码和结果。否则,你只会收集反对票。“字节类型将其转换为十进制格式”这句话毫无意义。字节是一个没有格式的数字;这只是你如何读入或读出一个数字,使之成为二进制或十进制或其他任何形式。[关于编辑]问题已经解决了,你可以保持这样。如果你真的需要删除问题,那么你应该标记它,选择“其他”,并向主持人解释为什么要删除它。字节将其转换为十进制格式,我想使用特定位置的位来执行所需的操作。@chettyharish没有看到您的代码,很难提供比我已经做的更多的信息;)我没有密码,因为我不知道从哪里开始。我必须对三个二进制数据进行编码,这些二进制数据不断移位,移位取决于特定的位值。比如说,如果第1个二进制数据的第7位有1,那么它会移动两次。我最初的评论仍然有效……我尝试了redFive,但大多数只是谈论移位运算符。在我的一个步骤中,我还必须改变一个特定的点,比如。X5=X1异或X2异或X3
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