简体   繁体   English

Java中的二进制表示

[英]Binary representation in Java

I am finding it difficult to understand and work with this binary representation in java: 我发现很难理解并使用java中的二进制表示:

With the help of the user Jon Skeet, I understood that binary representation should be built this way. 在用户Jon Skeet的帮助下,我明白应该以这种方式构建二进制表示。

Here's a code sample: 这是一个代码示例:

public class chack {

public static void main(String[] args) {
    int num2=2;
    int num3=3;
    int num4=4;
    int num1=1;
    int nirbinary = (num1 << 24) | (num2 << 16) | (num3 << 8) | num4;
    System.out.println(nirbinary);
    String nir=  Integer.toBinaryString(nirbinary);
    System.out.println(nir);
    }
}

Couple of question: 几个问题:

  1. How does one get num1 (for example) back from an int who is already in this binary 如何从已经在此二进制文件中的int返回num1(例如)
  2. why do I get 16909060 when I print nirbinary - what does it stands for? 为什么会16909060当我打印nirbinary -这是什么主张? How does one get num1 (for example) back from an int who is already in this binary representation? 如何从已经处于此二进制表示形式的int中获取num1(例如)?

Thank you 谢谢

I am not completely sure what you are missing, so I will just explain how you can convert integers to binary strings back and forth in java. 我不完全确定你缺少什么,所以我将解释如何在java中来回转换整数到二进制字符串。

You can get a binary string from an integer like so: 您可以从整数中获取二进制字符串,如下所示:

int i = 1234;
String binString = Integer.toBinaryString(i);

and you can convert the string back to an integer this way: 你可以用这种方式将字符串转换回整数:

int iNew = Integer.parseInt(binString, 2);

Note the second argument to Integer.parseInt() is the desired base of the number. 注意Integer.parseInt()的第二个参数是数字的期望基数。 2 is binary, 8 is octal, 10 decimal, etc. 2是二进制,8是八进制,10是十进制,等等。

16909060 stands for the number 16909060. 16909060代表数字16909060。

It is (1 * 2 24 ) + (2 * 2 16 ) + (3 * 2 8 ) + 4 . 它是(1 * 2 24 )+(2 * 2 16 )+(3 * 2 8 )+ 4

To get num1 back out, just right-shift the result the same amount you left-shifted and mask out the other bytes (not always necessary for num1 (*) , but for the others): 要让num1退出,只需右移结果与左移相同的数量并屏蔽掉其他字节(对于num1 (*)并非总是必需,但对于其他字节):

int num1 = nirbinary >> 24 & 0xFF;
int num2 = nirbinary >> 16 & 0xFF;
int num3 = nirbinary >> 8 & 0xFF;
int num4 = nirbinary & 0xFF;

Note that nirbinary is not "a binary representation". 请注意, nirbinary不是“二进制表示”。 Or more precisely: it's no more or less binary than num1 , num2 , num3 and num4 : internally all numbers (and characters, and booleans, ...) are stored in binary. 或者更准确地说:它不比num1num2num3num4更多或更少二进制:内部所有数字(和字符,布尔值......)都以二进制形式存储。

(*) note that if num1 is > 127, then you either need to use >>> to do the right-shift or use the & 0xFF in order to ensure that the correct value is restored. (*)注意,如果num1为> 127,那么你要么需要使用>>>做右移使用& 0xFF ,以确保正确的值将被恢复。 The difference between >> and >>> are the "new" bits inserted on the "left" side of the value: With >> they will depend on the highest-value bit (known as sign-extension) and with >>> they will always be 0. >>>>>之间的区别是在值的“左”侧插入的“新”位:使用>>它们将取决于最高值位(称为符号扩展)和>>>他们永远是0。

Here no need to depend only on binary or any other format... one flexible built in function is available That prints whichever format you want in your program.. 这里不需要只依赖于二进制或任何其他格式...一个灵活的内置函数可用于打印你想要的程序格式..

Integer.toString(int,representation);

Integer.toString(100,8)   // prints 144 --octal representation

Integer.toString(100,2)  // prints 1100100 --binary representation

Integer.toString(100,16) //prints 64  --Hex representation

Integer.toString(100,5) // prints 400  --Base 5

Every int is a number, it's not binary, hex or decimal, it's just a number. 每个int都是一个数字,它不是二进制,十六进制或十进制,它只是一个数字。 the statement (num1 << 24) | (num2 << 16) | (num3 << 8) | num4; 声明(num1 << 24) | (num2 << 16) | (num3 << 8) | num4; (num1 << 24) | (num2 << 16) | (num3 << 8) | num4; is a binary manipulation of 4 int s into another int. 是一个二进制操作4 int s到另一个int。 It doesn't change the representation of nirbinary to binary, since nirbinary has no representation, because (again) it's just a number. 它不会将nirbinary的表示形式nirbinary为二进制,因为nirbinary没有表示,因为(再次)它只是一个数字。

Integer.toBinaryString(nirbinary) returns the binary representation of nirbinary which means "how would nibinary look like in base-2". Integer.toBinaryString(nirbinary)返回的二进制表示nirbinary这意味着“如何将nibinary看起来像在碱-2”。

If you have a String which is a binary representation of a number, you could get its value, by using Integer.parseint(yourbinaryrepresentation, yourbase); 如果你有一个字符串,它是一个数字的二进制表示,你可以使用Integer.parseint(yourbinaryrepresentation, yourbase);获得它的值Integer.parseint(yourbinaryrepresentation, yourbase); for example - Integer.parseint(nir, 2); 例如 - Integer.parseint(nir, 2);

And another thing: 另一件事:

You can't always get back one of the numbers back from nirbinary, since you performed a bit manipulation that is not reversible, for example: 你不能总是从nirbinary返回一个数字,因为你执行了一些不可逆的操作,例如:

int i1 = 5;   //binary 0101
int i2 = 4;   //binary 0100
int i3 = i1 | i2;  //binary 0101

you cannot recognize each of your variables (i1, i2) since they have a common bit, i3 could have been the result of or on two other numbers: 你不能识别每个变量(I1,I2),因为他们有一个共同的一点,i3的本来的结果or对其他两个数字:

int i1 = 1;   //binary 0101
int i2 = 4;   //binary 0100
int i3 = i1 | i2;  //binary 0101

in your case, if each number is smaller than 256, you can reverse it with the following operation: 在您的情况下, 如果每个数字小于256,您可以通过以下操作将其反转:

int myoldnumber = (nirbinary >> previousShift) & 0xff;

for example, to retrieve num1 you can do: 例如,要检索num1您可以执行以下操作:

int retrievedNum1 = (nirbinary >> 24) & 0xff;

When working with bitshifting and integers I would recommend you think in hexadecimal numbers, that will usually make life a lot easier. 当使用位移和整数时,我建议你用十六进制数来思考,这通常会让生活变得更容易。 Just keep in mind that 8 bits represent 1 byte and 1 byte covers the hex-range from 0x00 to 0xFF 请记住,8位表示1字节,1字节覆盖从0x00到0xFF的十六进制范围

Since num1 to num4 are smaller than 10, their decimal representation is equal to their hex representiation, ie 1 = 0x01, 2 = 0x02 etc.. As I told you: 1 Byte is 8 bits. 由于num1到num4小于10,它们的十进制表示等于它们的十六进制表示,即1 = 0x01,2 = 0x02等。正如我告诉你的:1字节是8位。 In your bitshifting operation you always shift multiple of 8. 在您的位移操作中,您总是移动8的倍数。

  • So 0x01 << 8 => 0x0100 所以0x01 << 8 => 0x0100
  • 0x01 << 16 => 0x010000 0x01 << 16 => 0x010000
  • etc. 等等

So you basically only add zero bytes, which of course increases the value. 所以你基本上只添加零字节,这当然会增加值。 What you do next is to | 你接下来要做的是| them, a bitwise or. 他们,有点或。 This means that two bitfields get modified in such a way that the result has a 1 at one place if at least one of the input values as a 1 there. 这意味着两个位域被修改,使得如果输入值中的至少一个作为1,则结果在一个位置处具有1。 Since your shifted ints contain only zero at the back, a bitwise or is nothing else then to put the value in this spot. 由于您的移位整数在后面只包含零,因此按位或者只是将值放在此位置。 Eg: 例如:

(0x01 << 8) | 0x02

0x01 << 8 will produce 0x0100 . 0x01 << 8将产生0x0100 Now you simply have to replace the last 00 with 02, since you or them: 0x0102 现在你只需用02替换最后一个00,因为你或他们: 0x0102

If you want to recreate the original int, you have to mask the part that int represents (this is easy since the parts do not overlap in your example) and then shift it back. 如果要重新创建原始int,则必须屏蔽int表示的部分(这很容易,因为部件在您的示例中不重叠)然后将其移回。

Eg Say ou produced 0x010203 and want to have only 0x02. 例如,说你产生了0x010203并且想要只有0x02。 You now have to mask shift it back 0x010203 >> 8 which will put the 02 in the last part. 你现在必须掩盖它将它移回0x010203 >> 8,这将把02放在最后一部分。 Now simply mask this last part 0x0102 && 0xFF. 现在只需屏蔽最后一部分0x0102 && 0xFF。 This will set all but the last 8 bits to zero 这会将除最后8位之外的所有位设置为零

  1. it's basically 1 * 2^24 + 2 * 2^16 + 3 * 2^8 + 4 = 16909060 它基本上是1 * 2^24 + 2 * 2^16 + 3 * 2^8 + 4 = 16909060
  2. You can get num1 by doing num1 = nirbinary >> 24 . 你可以通过num1 = nirbinary >> 24获得num1。
  1. What did you expect instead? 你有什么期望呢?
  2. To get the most significant byte from an int i: 要从int i中获取最重要的字节:

(i >> 24) & 0xff (i >> 24)&0xff

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM