Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binary representation in Java

Tags:

java

binary

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:

  1. How does one get num1 (for example) back from an int who is already in this binary
  2. why do I get 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

like image 251
Unknown user Avatar asked Apr 08 '11 09:04

Unknown user


People also ask

What is binary representation?

Binary is a base-2 number system that uses two states 0 and 1 to represent a number. We can also call it to be a true state and a false state. A binary number is built the same way as we build the normal decimal number. For example, a decimal number 45 can be represented as 4*10^1+5*10^0 = 40+5.

How do you find the representation of a binary number?

To convert integer to binary, start with the integer in question and divide it by 2 keeping notice of the quotient and the remainder. Continue dividing the quotient by 2 until you get a quotient of zero. Then just write out the remainders in the reverse order.


2 Answers

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.

like image 84
Joachim Sauer Avatar answered Nov 07 '22 02:11

Joachim Sauer


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.

like image 40
jberg Avatar answered Nov 07 '22 03:11

jberg