Thread: Bitwise And

  1. #16
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Alan Gott View Post
    Wouldn't that involve doing the calculations in display_binary then return them to main? (If not, I'm totally clueless to what you are trying to teach me. :S
    No. What I was getting at is this (note, these numbers are not correct for your example, you have to modify them):
    Code:
    command = (number & 0xF000) >> 12;
    & with 0xF000 leaves the left 4 bits (0xF000 = 1111 0000 0000 0000) the way they are and clears the rest of them. The >> 12 moves everything to the right 12 places, so that you have numbers, from, say 0-8 instead of 8192-57344.

  2. #17
    C Newbie
    Join Date
    Oct 2011
    Posts
    59
    Quote Originally Posted by CommonTater View Post
    No...

    You had to do all that looping to display the binary value underlying your variable.

    Bitwise and ( & ) operates directly on that underlying value.

    Do a little reading on the logic functions AND OR NEG and XOR for a better insight.
    Ahh, that clears it up. Thanks!

    Quote Originally Posted by anduril462 View Post
    No. What I was getting at is this (note, these numbers are not correct for your example, you have to modify them):
    Code:
    command = (number & 0xF000) >> 12;
    & with 0xF000 leaves the left 4 bits (0xF000 = 1111 0000 0000 0000) the way they are and clears the rest of them. The >> 12 moves everything to the right 12 places, so that you have numbers, from, say 0-8 instead of 8192-57344.
    Thanks, that makes sense.

  3. #18
    C Newbie
    Join Date
    Oct 2011
    Posts
    59
    Sorry to bump this thread again, but I have ONE more question. When I am trying to identify the switch, something is happening incorrectly. I have:
    Code:
    switch = (number & 0xF000) >> 7;    
    if(switch == 0)
            printf("     Switch: OFF");
    else 
            printf("     Switch: ON");
    Where the switch is determined by the 8th digit from the right, 1 being on and 0 being off. But, when I enter 220, or 1101 1100, it is telling me that the switch is off. Any ideas as why?
    Last edited by Alan Gott; 11-22-2011 at 02:27 PM.

  4. #19
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You are applying the wrong value...

    What hex value puts a 1 in only bit 7? (which is the 8th bit from the right)

    Opens Win7 calculator, Programmer mode... click bit 7... = 0x80

    Thus...
    Code:
    if (number & 0x80)
      puts("Eyyup, it's on!");
    else
      puts("Where's that light switch, Martha?");

  5. #20
    C Newbie
    Join Date
    Oct 2011
    Posts
    59
    Quote Originally Posted by CommonTater View Post
    You are applying the wrong value...

    What hex value puts a 1 in only bit 7? (which is the 8th bit from the right)

    Opens Win7 calculator, Programmer mode... click bit 7... = 0x80

    Thus...
    Code:
    if (number & 0x80)
      puts("Eyyup, it's on!");
    else
      puts("Where's that light switch, Martha?");
    So does that mean I am also finding the command wrong?
    Code:
    command = (number & 0xF000) >> 13;    
    printf("    Command: %d\n", command);
    The above works, but maybe what I need is a link to a thorough explanation of things such as 0xF and 0x80.
    Last edited by Alan Gott; 11-22-2011 at 03:04 PM.

  6. #21
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Alan Gott View Post
    So does that mean I am also finding the command wrong?
    Code:
    command = (number & 0xF000) >> 13;    
    printf("    Command: %d\n", command);
    Given that the command is supposed to be only the 3 msb (most significant bits) yes...

    You probably want to AND with 0xE000 instead of ... 0xF000

    However! ... since you are looking only for the three most significant bits, there's no need to strip lower bits with AND at all... You can just send them to the bit-bucket.
    Code:
    command = number >> 13:
    will get the job done.


    Something you will find very handy when bit twiddling is to take a second and write down the binary values for 0 to F ... then you have a visual reference that can show you what values you need.
    Last edited by CommonTater; 11-22-2011 at 03:05 PM.

  7. #22
    C Newbie
    Join Date
    Oct 2011
    Posts
    59
    Quote Originally Posted by CommonTater View Post
    Given that the command is supposed to be only the 3 msb (most significant bits) yes...

    You probably want to AND with 0xE000 instead of ... 0xF000

    However! ... since you are looking only for the three most significant bits, there's no need to strip lower bits with AND at all... You can just send them to the bit-bucket.
    Code:
    command = number >> 13:
    will get the job done.


    Something you will find very handy when bit twiddling is to take a second and write down the binary values for 0 to F ... then you have a visual reference that can show you what values you need.
    Thanks a ton! I will definitely look into this more.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with bitwise AND
    By tehjojo in forum C Programming
    Replies: 1
    Last Post: 04-03-2010, 02:23 PM
  2. Bitwise 'and' vs '=='
    By Ducky in forum Windows Programming
    Replies: 4
    Last Post: 07-27-2009, 12:37 PM
  3. 64 Bitwise AND
    By Lostsheep in forum C++ Programming
    Replies: 2
    Last Post: 06-16-2008, 12:17 PM
  4. Bitwise OR
    By tinkerbell20 in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2005, 02:23 AM
  5. bitwise help
    By linuxdude in forum C Programming
    Replies: 8
    Last Post: 11-20-2003, 08:37 PM