I am not able to understand why in the following program it is mentioned in my book:
The value of ~a is ANDed with 0x0f (0000 1111) in binary in order to reduce its value to less than 16, so it can be printed by use of the binary array.

class bitLogic
{
	public static void main(String args[])
	{
		String binary[] = { "0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111" };
		int a=3;
		int b=6;
		int c=a | b;
		int d=a & b;
		int e=a ^ b;
		int f=(~a&b) | (a&~b);
		int g=~a& 0x0f;

		System.out.println("           a = "+ binary[a]);		
		System.out.println("           b = "+ binary[b]);
		System.out.println("        a|b = "+ binary[c]);
		System.out.println("        a&b = "+ binary[d]);
		System.out.println("        a^b = "+ binary[e]);
		System.out.println("(~a&b)|(a&~b)= "+ binary[f]);
		System.out.println("          ~a = "+ binary[g]);
	}
}

Could someone please tell me how to we come to know which binary number representations like 0x0f , 0x0f1 , 0x0ff..etc. represent.

Recommended Answers

All 2 Replies

I think these numbers (0x0f , 0x0f1 , 0x0ff) are not binaries, they're hexadecimal.
but to convert binary to hexadecimal you can use table that convert 4 digits binary, like :
0000 -> 0
0001 -> 1
0010 -> 2
0011 -> 3
0100 -> 4
0101 -> 5
0110 -> 6
0111 -> 7
1000 -> 8
1001 -> 9
1010 -> A
1011 -> B
1100 -> C
1101 -> D
1110 -> E
1111 -> F

so, if you found binary like : 1001 1011, just use the table and convert it to 9B.

Well, I'm not quite sure about the tilde, but it looks like it's using 0x0f as a mask which basically cancels outs the higher 4 bits of a byte. By canceling out all but the lower 4 bits, the resulting number will range between 0 and 15. With all the variables being integers(4 bytes), I would think the masking hexadecimal would be longer. Or another possibility is I'm completely wrong. I'm not positive on my answer, so careful how you take it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.