Java – Convert negative binary to Integer

Review the following Java example to convert a negative integer in binary string back to an integer type. String binary = Integer.toBinaryString(-1); // convert -1 to binary // 11111111 11111111 11111111 11111111 (two’s complement) int number = Integer.parseInt(binary, 2); // convert negative binary back to integer System.out.println(number); // output ?? The result is NumberFormatException! Exception …

Read more

Java >> and >>> bitwise shift operators

In programming, bitwise shift operators, >> means arithmetic right shift, >>> means logical right shift, the differences: >>, it preserves the sign (positive or negative numbers) after right shift by n bit, sign extension. >>>, it ignores the sign after right shift by n bit, zero extension. To work with bitwise shift operators >> and …

Read more