How to get the lower 8 bits of an int in Java

1 Answer

0 votes
public class Program
{
	public static void main(String[] args)
	{
		int n = 1957;

		System.out.println(String.format("%16s", Integer.toBinaryString(n)).replaceAll(" ", "0"));

		int low8bits = (int)(n & 0xFF);

		System.out.println(String.format("%16s", Integer.toBinaryString(low8bits)).replaceAll(" ", "0"));
	}
}




/*
run:

0000011110100101
0000000010100101

*/

 



answered Apr 3, 2024 by avibootz

Related questions

1 answer 183 views
1 answer 192 views
1 answer 185 views
1 answer 169 views
1 answer 164 views
1 answer 190 views
1 answer 178 views
...