Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.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_Integer_Hex_Bitwise Operators - Fatal编程技术网

Java 将两个整数保存为一个整数

Java 将两个整数保存为一个整数,java,integer,hex,bitwise-operators,Java,Integer,Hex,Bitwise Operators,将两个整数保存为一个整数指定如下: 底部字节包含区块使用的扇区数。其他三个 字节包含块扇区的偏移量 所以如果我想提取我的两个数字,我必须 int usedSectors = num & 0xFF; int sectorOffset = num >> 8; 但是如何将两个整数保存到一个整数中,例如,我已经使用了给定的扇区和扇区偏移(我们假设UsedSector的范围仅为0到255之间的1字节,扇区偏移的范围为0到16777215之间的3字节) aggrigaated_int

将两个整数保存为一个整数指定如下:

底部字节包含区块使用的扇区数。其他三个 字节包含块扇区的偏移量

所以如果我想提取我的两个数字,我必须

int usedSectors  = num & 0xFF;
int sectorOffset = num >> 8;
但是如何将两个整数保存到一个整数中,例如,我已经使用了给定的扇区和扇区偏移(我们假设UsedSector的范围仅为0到255之间的1字节,扇区偏移的范围为0到16777215之间的3字节)

aggrigaated_int=0;
聚合整数+=第一个整数;
aggrigaated_int+=second_num
aggrigaated_int=0;
聚合整数+=第一个整数;
聚合整数+=第二个测试程序:

public class ByteTest {

  public static void main(String[] args) {
      int expectedUsedSectors = 4;
      int expectedSectorOffset = 20000;

      int num = expectedSectorOffset;
      num = num << 8;
      num |= expectedUsedSectors;

      int usedSectors  = num & 0xFF;
      int sectorOffset = num >> 8;

      System.out.println("used sectors expected == actual? " + (expectedUsedSectors == usedSectors));
      System.out.println("offset expected == actual? " + (expectedSectorOffset == sectorOffset));
    }
}
公共类ByteTest{
公共静态void main(字符串[]args){
int预期扇区=4;
int expectedSectorOffset=20000;
int num=预期的扇区偏移量;
num=num>8;
System.out.println(“预期使用的扇区==实际?”+(预期使用的扇区==使用的扇区));
System.out.println(“预期偏移量==实际偏移量?”+(预期扇区偏移量==扇区偏移量));
}
}
首先设置3字节值,然后移位8位,然后设置第二个数字。

测试程序:

public class ByteTest {

  public static void main(String[] args) {
      int expectedUsedSectors = 4;
      int expectedSectorOffset = 20000;

      int num = expectedSectorOffset;
      num = num << 8;
      num |= expectedUsedSectors;

      int usedSectors  = num & 0xFF;
      int sectorOffset = num >> 8;

      System.out.println("used sectors expected == actual? " + (expectedUsedSectors == usedSectors));
      System.out.println("offset expected == actual? " + (expectedSectorOffset == sectorOffset));
    }
}
公共类ByteTest{
公共静态void main(字符串[]args){
int预期扇区=4;
int expectedSectorOffset=20000;
int num=预期的扇区偏移量;
num=num>8;
System.out.println(“预期使用的扇区==实际?”+(预期使用的扇区==使用的扇区));
System.out.println(“预期偏移量==实际偏移量?”+(预期扇区偏移量==扇区偏移量));
}
}

首先我设置3字节的值,然后移位8位,然后设置第二个数字。

您刚才给出了一个“如何将两个整数保存到一个整数中”的示例,那么您有什么疑问?是因为您有这些说明,但您不理解它们吗?我尝试了以下方法,但如果sectorOffset>256,我就遇到了问题。字节[]头=新字节[4];标头[3]=扇区asbyte[3];标题[2]=偏移字节[3];标头[1]=扇区asbyte[2];标头[0]=扇区ASByte[1];它不应该是
num>>8
?否则,有时会出现负扇区偏移。您刚才给出了一个例子,“如何将两个整数保存到一个整数中”,那么您有什么疑问?是因为您有这些说明,但您不理解它们吗?我尝试了以下方法,但如果sectorOffset>256,我就遇到了问题。字节[]头=新字节[4];标头[3]=扇区asbyte[3];标题[2]=偏移字节[3];标头[1]=扇区asbyte[2];标头[0]=扇区ASByte[1];它不应该是
num>>8
?否则,有时会出现负扇区偏移。或
(second\u num)或
(second\u num