Search code examples
c++arduinobit-shiftesp32

use of enum on shift bytes in c/c++


I want to create specific byte - uint8_t to set it in a register.

This byte consists from 2 sections. Bits <7:6> is a multiplication factor.

These 2 bits can be: 00 for multiplication with 1 01 for multiplication with 4 10 for multiplication with 16 11 for multiplication with 64

for these 2 bits i am using an enum like:

typedef enum multiFactorEnum{
FACTOR_1,
FACTOR_4,
FACTOR_16,
FACTOR_64
}MultiFactor;

For the rest bytes it can be whatever number from 1 - 63 (representing time) since this is the value if you set the bit <5:0> to 1.

So when i know what factor i should use i am creation the value as

uint8_t val = (MultiFactor::FACTOR16 << 6 ) | timeValue;

This is giving me an issue:

'<<' in boolean context, did you mean '<'

I tried to cast the enum to uint8_t but it is not correct.


Solution

  • typedef enum{
        FACTOR_1 = 0,
        FACTOR_4 = 1 << 6,
        FACTOR_16 = 2 << 6,
        FACTOR_64 = 3 << 6,
    }MultiFactor;
    
    uint8_t val = MultiFactor::FACTOR_16 | timeValue;
    

    if you need to clear bits 6:7 in the time value before ORing

    uint8_t val = MultiFactor::FACTOR_16 | (timeValue & ~MultiFactor::FACTOR_64);