Results 1 to 4 of 4
  1. #1
    Klenberg is offline Member
    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0

    Default 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

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.

  3. #3
    Klenberg is offline Member
    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0

    Default 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?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

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

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 2
    Last Post: 04-02-2011, 12:57 PM
  2. Replies: 13
    Last Post: 03-22-2011, 06:49 AM
  3. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 06:53 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

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