I know I can convert this:

```
my_var |= (1<<7);
my_var |= (1<<6);
```

into

```
my_var |= ((1<<6)|(1<<7));
```

Is there any way to do the same with this?

```
my var &= ~(1<<7);
my_var |= (1<<6);
```

Thanks

I know I can convert this:

```
my_var |= (1<<7);
my_var |= (1<<6);
```

into

```
my_var |= ((1<<6)|(1<<7));
```

Is there any way to do the same with this?

```
my var &= ~(1<<7);
my_var |= (1<<6);
```

Thanks

```
myvar = myvar & ~(1 << 7) | (1 << 6);
```

But for readability I wouldn’t use the one line notation. It’s not very simplified. Writing it in multiple lines is more clear.

For the first example you can also do

```
myVar |= 0b11000000;
//or
myVar |= 0xC0;
```

But key is it must be clear code.

Awsome.

I normally use _BV(x) for readability. Of course not every single compiler works with it.

I would not use the shift notation unless the bit numbers are hidden behind a const or #define, i.e.:

myvar |= (1<<SOME_BIT_NAME_THAT_MAKES_SENSE);

KeithRB:

I would not use the shift notation unless the bit numbers are hidden behind a const or #define, i.e.:

myvar |= (1<<SOME_BIT_NAME_THAT_MAKES_SENSE);

What would you use then?

The binary or hex value that septillion exampled.

Hex. The binary representation isn't portable outside Arduinia.

If you are using bit numbers it isn't very portable, anyway. But you could use the B00000000 notation, too.

KeithRB:

The binary or hex value that septillion exampled.

Agreed, but the point of bitwise operations is if I want to toggle/set/clear a bit without knowing or changing the other bits.

For example this loop:

```
PORTB = 0b01000000;
for(i=2;i<8;i++)
{
PORTB = (PORTB>>i);
delayms(500); // Slow it down
if(my_value>500){
myport2b ^= (1<<4); // Toggle LED 4
PORTB = myport2b; // Update lights
}
}
```

With 5 of them one might not exactly know or want to change the bits in a whole

Which has nothing to do with what I or septillion wrote.

If you are calculating something at run time, you would need different syntax.

casemod:

IIs there any way to do the same with this?`my var &= ~(1<<7);`

my_var |= (1<<6);

If you know for sure that bit 7 is set and bit 6 is, then you can flip them with the bit-flipping (XOR) operator:

```
my_var ^= (1<<7) | (1<<6);
```

or just plain

```
my_var ^= 0b11000000;
```