UPD . The author dramatically changed the question, so the answer can be read immediately with UPD1 .
For example:
System.out.println(Integer.toBinaryString(192));
And why do you convert int to byte - very incomprehensible.
Ps. The Integer.toBinaryString(...) method returns a string without leading zeros. That is, when performing:
System.out.println(Integer.toBinaryString(1));
You'll get:
1
If you need zeros at the beginning of a line, you can, for example, do this:
System.out.println(String.format("%8s", Integer.toBinaryString(1)).replace(' ', '0'));
UPD 1 .
byte is an 8-bit type ( with a sign ), its range is [-(2^8)/2;(2^8)/2 - 1] , that is, [-128;127] .
Perform conversion:
byte b = (byte) 192;
happens as follows:
Since 192 does not fit in byte , the remainder of dividing the number 192 by the range of byte values is calculated:
192 % 2^8 = 192
Since the remainder of the division still does not fit into the byte , then the size of the byte range is subtracted from the remainder of the division, that is, from the number 192 :
192 - 256 = -64
As a result, the variable b will contain the value -64 .