1. ## 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. 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. 3. Originally Posted by Nikosant03 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. 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. ## 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``` 6. Originally Posted by Click_here 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. Well... Bit inversion is missing:

Code:
`x ^= (1 << bit);` 8. Originally Posted by flp1969 Well... Bit inversion is missing:

Code:
`x ^= (1 << bit);`
*Like* Popular pages Recent additions bitwise, channel, fact, understand, works 