Thread: Bitwise operators

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    97

    Bitwise operators

    Hi, could someone explain me how the if statement works in the snippet below? Despite the fact that I know how bitwise works I cannot understand when the
    Code:
     channel & (1 << i)
    will be TRUE and when will be FALSE.



    Code:
    void selectMuxPin(int channel) {  
    for (int i = 0; i < MUX_CTRL_CHA; i++) {
        
        if (channel & (1 << i)) {
          nrf_gpio_pin_set(ctrlPins[i]);
          }else {
          nrf_gpio_pin_clear(ctrlPins[i]);
        }
      }
    }

  2. #2
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    It will result in true when the bit corresponding (2^i) in channel is set to 1 (where, '^' denotes "raised to the power of" and not XOR). Otherwise, false.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by Nikosant03 View Post
    Hi, could someone explain me how the if statement works in the snippet below? Despite the fact that I know how bitwise works I cannot understand when the
    Code:
     channel & (1 << i)
    Like Zeus_ said... Here's a detailed explanation.

    When testing for true or false, any value different from 0 is TRUE. 0 is always FALSE. This expression will isolate bit i of the value from channel, all the other bits will be zero. If i is 3, for example, (1 << i) will be, in binary, 0b00000000000000000000000000001000. After the AND operation all bits are zero, except the 4th bit from right to left (bit 3), which will be a copy from the same bit from channel. If the 4th bit of channel is 0, the result will be 0, otherwise, 0x0004.

    Got it?

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    97
    Thank you guys, now I can see how it works. Basically anything besides 000 is a TRUE isn't it? I analyzed the function and the results are clear

    MUX_CTRL_CHA = 3
    1 = 001


    channel = 0(000) then:
    For i = 0 -> 001 << 0 = 001 , channel & (1 << i) -> 000 & 001 = 000 -> nrf_gpio_pin_clear 0
    For i = 1 -> 001 << 1 = 010 , channel & (1 << i) -> 000 & 010 = 000 -> nrf_gpio_pin_set 0
    For i = 2 -> 001 << 2 = 100 , channel & (1 << i) -> 000 & 100 = 000 -> nrf_gpio_pin_clear 0


    channel = 1(001) then:
    For i = 0 -> 001 << 0 = 001 , channel & (1 << i) -> 001 & 001 = 001 -> nrf_gpio_pin_set 1
    For i = 1 -> 001 << 1 = 010 , channel & (1 << i) -> 001 & 010 = 000 -> nrf_gpio_pin_clear 0
    For i = 2 -> 001 << 2 = 100 , channel & (1 << i) -> 001 & 100 = 000 -> nrf_gpio_pin_clear 0


    channel = 2 (010) then:
    For i = 0 -> 001 << 0 = 001 , channel & (1 << i) -> 010 & 001 = 000 -> nrf_gpio_pin_clear 0
    For i = 1 -> 001 << 1 = 010 , channel & (1 << i) -> 010 & 010 = 010 -> nrf_gpio_pin_set 1
    For i = 2 -> 001 << 2 = 100 , channel & (1 << i) -> 010 & 100 = 000 -> nrf_gpio_pin_clear 0


    channel = 3 (011) then:
    For i = 0 -> 001 << 0 = 001 , channel & (1 << i) -> 011 & 001 = 001 -> nrf_gpio_pin_set 1
    For i = 1 -> 001 << 1 = 010 , channel & (1 << i) -> 011 & 010 = 010 -> nrf_gpio_pin_set 1
    For i = 2 -> 001 << 2 = 100 , channel & (1 << i) -> 011 & 100 = 000 -> nrf_gpio_pin_clear 0


    channel = 4 (100) then:
    For i = 0 -> 001 << 0 = 001 , channel & (1 << i) -> 100 & 001 = 000 -> nrf_gpio_pin_clear 0
    For i = 1 -> 001 << 1 = 010 , channel & (1 << i) -> 100 & 010 = 000 -> nrf_gpio_pin_clear 0
    For i = 2 -> 001 << 2 = 100 , channel & (1 << i) -> 100 & 100 = 100 -> nrf_gpio_pin_set 1


    channel = 5 (101) then:
    For i = 0 -> 001 << 0 = 001 , channel & (1 << i) -> 101 & 001 = 001 -> nrf_gpio_pin_set 1
    For i = 1 -> 001 << 1 = 010 , channel & (1 << i) -> 101 & 010 = 000 -> nrf_gpio_pin_clear 0
    For i = 2 -> 001 << 2 = 100 , channel & (1 << i) -> 101 & 100 = 100 -> nrf_gpio_pin_set 1


    channel = 6 (110) then:
    For i = 0 -> 001 << 0 = 001 , channel & (1 << i) -> 110 & 001 = 000 -> nrf_gpio_pin_clear 0
    For i = 1 -> 001 << 1 = 010 , channel & (1 << i) -> 110 & 010 = 010 -> nrf_gpio_pin_set 1
    For i = 2 -> 001 << 2 = 100 , channel & (1 << i) -> 110 & 100 = 100 -> nrf_gpio_pin_set 1


    channel = 7 (111) then:
    For i = 0 -> 001 << 0 = 001 , channel & (1 << i) -> 111 & 001 = 001 -> nrf_gpio_pin_set 1
    For i = 1 -> 001 << 1 = 010 , channel & (1 << i) -> 111 & 010 = 010 -> nrf_gpio_pin_set 1
    For i = 2 -> 001 << 2 = 100 , channel & (1 << i) -> 111 & 100 = 100 -> nrf_gpio_pin_set 1

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907

    Cool

    Basically anything besides 000 is a TRUE isn't it?
    Yes.

    This is a basic bit test.
    Code:
    // A few simple bit tasks for u-controllers...
    
    // Test bit
    (1<<bit) & x
    
    // Set bit
    x |= (1<<bit);
    
    // Set multiple bits
    x |= (1<<bit1) | (1<<bit2) | (1<<bit3);
    
    // Clear bit
    x &= ~(1<<bit);
    
    // Clear multiple bits
    x &= ~((1<<bit1) | (1<<bit2) | (1<<bit3));
    An easier example would be if I had a microcontroller with an 8-bit port, PORTA.

    On the input for b3 (usually corresponds to the forth pin, because b0 is the first pin) I have a switch that pulls the pin to logic level high and we want a motor to start...
    Code:
    if ((1<<3) & PORTA)
      StartMotor();
    In the AVR world you see a lot of the "<<" notation to set and clear bits. I tend to do it with all different microcontroller family projects now days...

    This is so that you can see each individual bit.

    Code:
    x = (1<<0) | //comment
        (1<<1) | //comment
        (0<<2) ; //comment
    You can see that the 0<< is kept in, just so a comment can be made as to why is was 0.

    This is from a PIC, setting up the oscillator - Note that 0b is a non-standard but widely used way of writing binary literal and the _xx_POSITION is a defined number (0-7) in a header file for the XC8 compiler.
    Code:
    OSCCON =  (0b1110 << _OSCCON_IRCF_POSITION)  |   // 8 MHz or 32 MHz HF(see Section 5.2.2.1 ?HFINTOSC?)
              (0b00 << _OSCCON_SCS_POSITION)     |   // Clock determined by FOSC<2:0> in Configuration Word 1
              (0 << _OSCCON_SPLLEN_POSITION);        // 4x PLL Is enabled
    Fact - Beethoven wrote his first symphony in C

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    97
    Quote Originally Posted by Click_here View Post
    Yes.

    This is a basic bit test.
    Code:
    // A few simple bit tasks for u-controllers...
    
    // Test bit
    (1<<bit) & x
    
    // Set bit
    x |= (1<<bit);
    
    // Set multiple bits
    x |= (1<<bit1) | (1<<bit2) | (1<<bit3);
    
    // Clear bit
    x &= ~(1<<bit);
    
    // Clear multiple bits
    x &= ~((1<<bit1) | (1<<bit2) | (1<<bit3));


    [/code]
    Great answer!! Thank you for the sample code!!

  7. #7
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Well... Bit inversion is missing:

    Code:
    x ^= (1 << bit);

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by flp1969 View Post
    Well... Bit inversion is missing:

    Code:
    x ^= (1 << bit);
    *Like*
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise operators in C.
    By RyanC in forum C Programming
    Replies: 1
    Last Post: 02-19-2016, 06:00 PM
  2. Bitwise Operators
    By (^Burt^) in forum C Programming
    Replies: 4
    Last Post: 10-24-2013, 01:20 AM
  3. Bitwise operators help ~
    By Siaw Ys in forum C Programming
    Replies: 7
    Last Post: 12-11-2011, 02:02 AM
  4. bitwise operators
    By manzoor in forum C++ Programming
    Replies: 12
    Last Post: 08-18-2008, 02:43 PM
  5. bitwise operators?
    By EvilPickles in forum C++ Programming
    Replies: 18
    Last Post: 08-07-2006, 08:53 AM

Tags for this Thread