# Thread: How is the Value of ~a obtained in the below program?

1. Member Join Date
Sep 2012
Posts
2
Rep Power
0

## How is the Value of ~a obtained in the below program?

The following program demonstrates the Bitwise logical operators:

//Demonstrate the Bitwise logical operators

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; // 0 + 2 + 1 or 0011 in binary

int b = 6; // 4 + 2 + 0 or 0011 in binary

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[a]);

System.out.println( a & b =  + binary[a]);

System.out.println( a ^ b =  + binary[a]);

System.out.println(~a&b | a&~b =  + binary[a]);

System.out.println( ~a =  + binary[a]);

}

}

In this example, a and b has bit patterns, which present all four possibilities for two binary digits: 0-0, 0-1, 1-0, and 1-1. You can see how the | and & operate on each bit by the results in c and d. The values assigned to e and f are the same and illustrate how the ^ works. The string array named binary holds the human-readable, binary representation of the numbers 0 through 15. in this example, the array is indexed to show the binary representation of each result. The array is constructed such that the correct string representation of a binary value n is stored in binary [n]. 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. Here is the output from this program.

a = 0011

b = 0011

a | b = 0011

a & b = 0011

a ^ b = 0011

~a&b | a&~b = 0011

~a = 0011

======================================

I haven't understood how the value of ~a = 0011 is obtained in the output. Could anyone explain it in detail/stepwise. I would highly appreciate that.

What I have understood is:

0x0f = 0000 1111 [ I'm assuming 0x is just for denoting hexdecimal number]

and ~a = 1111

Therefore it should be:

0000 1111
& 1111
--------------
0000 1111  Reply With Quote

2. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
17

## Re: How is the Value of ~a obtained in the below program?

All the answers come out the same! I might have expected 101010, but not 0011.

Look at the code to see why everything is coming out =0011.  Reply With Quote

3. Member Join Date
Sep 2012
Posts
2
Rep Power
0

## Re: How is the Value of ~a obtained in the below program?

Actually it should be like the following:

~a = 1100

0x0f = 0000 1111

Therefore, ~a & 0x0f = 0000 1111
& 1100
_____________
0000 1100

Answer should be 0000 1100, isn't it?  Reply With Quote

4. ## Re: How is the Value of ~a obtained in the below program?  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•