Reputation: 45301
I am finding it difficult to understand and work with this binary representation in java:
With the help of the user Jon Skeet, I understood that binary representation should be built this way.
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:
16909060
when I print nirbinary
- what does it stands for?
How does one get num1 (for example) back from an int who is already in this binary
representation?Thank you
Upvotes: 12
Views: 61353
Reputation: 4818
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.
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. 2 is binary, 8 is octal, 10 decimal, etc.
Upvotes: 28
Reputation: 4015
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
Upvotes: 3
Reputation: 308001
16909060
stands for the number 16909060.
It is (1 * 224) + (2 * 216) + (3 * 28) + 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):
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". 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.
(*) 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. 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.
Upvotes: 14
Reputation: 137272
Every int
is a number, it's not binary, hex or decimal, it's just a number. the statement (num1 << 24) | (num2 << 16) | (num3 << 8) | num4;
is a binary manipulation of 4 int
s into another int. It doesn't change the representation of nirbinary
to binary, since nirbinary has no representation, because (again) it's just a number.
Integer.toBinaryString(nirbinary)
returns the binary representation of nirbinary
which means "how would nibinary look like in base-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);
for example - 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:
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:
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:
int myoldnumber = (nirbinary >> previousShift) & 0xff;
for example, to retrieve num1
you can do:
int retrievedNum1 = (nirbinary >> 24) & 0xff;
Upvotes: 3
Reputation: 2060
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
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. In your bitshifting operation you always shift multiple of 8.
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. Since your shifted ints contain only zero at the back, a bitwise or is nothing else then to put the value in this spot. E.g:
(0x01 << 8) | 0x02
0x01 << 8
will produce 0x0100
. Now you simply have to replace the last 00 with 02, since you or them: 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.
E.g. Say ou produced 0x010203 and want to have only 0x02. You now have to mask shift it back 0x010203 >> 8 which will put the 02 in the last part. Now simply mask this last part 0x0102 && 0xFF. This will set all but the last 8 bits to zero
Upvotes: 2
Reputation: 36329
(i >> 24) & 0xff
Upvotes: 0
Reputation: 88707
1 * 2^24 + 2 * 2^16 + 3 * 2^8 + 4 = 16909060
num1 = nirbinary >> 24
.Upvotes: 0