Doubt on how to use Shift in Java

1

Well, I have a question on how to use Shift Left in java. I have a String representing a value in hexadecimal. Except I need to pass this value to binary. For this I use an integer variable to turn this String into a decimal number:

int primeiroOperando = Integer.parseInt(addR2, 16);

And to turn into binary, I use toBinaryString:

String addR1 = Integer.toBinaryString(primeiroOperando);

But as I need to do the Shift, because I have to get the least significant bits, I can not use this String since it has to use a whole number for this. How can I do this binary conversion to save to an integer variable so I can do the Shift?

    
asked by anonymous 27.03.2017 / 03:14

1 answer

1

The binary is just a representation. You can use shift with decimal, hexadecimal, etc., as shown in the example below:

    int i = 7;
    int x = i >> 1;
    int y = i << 2;
    System.out.println(String.format("i=%d, x=%d, y=%d", i, x, y)); // i=7, x=3, y=28
    
27.03.2017 / 14:21