Home > OS >  Arrays of enum's packed into bit fields in MSVC
Arrays of enum's packed into bit fields in MSVC

Time:02-26

Unsing MS Studio 2022 I am trying to pack two items into a union of size 16 bits but I am having problems with the correct syntax.

The first item is an unsigned short int so no problems there. The other is an array of 5 items, all two bits long. So imagine:

enum States {unused, on, off}; 
// Should be able to store this in a 2 bit field

then I want

States myArray[5]; 
// Should be able to fit in 10 bits and 
// be unioned with my unsigned short

Unfortunatly I am completely failing to work out the correct syntax which leads to my array fitting into 16 bits. Any ideas?

CodePudding user response:

You can't do that. An array is an array, not some packed bits.

What you can do is using manual bit manipulation:

#include <iostream>
#include <cstdint>
#include <bitset>
#include <climits>
enum status {
    on = 0x03,
    off = 0x01,
    unused = 0x00
};
constexpr std::uint8_t status_bit_width = 2;


std::uint16_t encode(status s,std::uint8_t id, std::uint16_t status_vec) {

    if(id >= (CHAR_BIT * sizeof(std::uint16_t)) / status_bit_width) {
        std::cout << "illegal id" << std::endl;
        return status_vec;
    }
    std::uint8_t bit_value = s;
    status_vec |= bit_value << (id*status_bit_width);
    return status_vec;
};

int main(void) {
    std::uint16_t bits = 0;
    bits = encode(on,1,bits);
    std::cout << std::bitset<16>(bits) << std::endl;
    bits = encode(off,2,bits);
    std::cout << std::bitset<16>(bits) << std::endl;
    bits = encode(unused,3,bits);
    std::cout << std::bitset<16>(bits) << std::endl;
    bits = encode(off,4,bits);
    std::cout << std::bitset<16>(bits) << std::endl;
    bits = encode(off,7,bits);
    std::cout << std::bitset<16>(bits) << std::endl;
    bits = encode(on,8,bits);
}

CodePudding user response:

Thanks to all who tried to help me and for clarifying my mis-understanding of what an array is.

Thanks to Raildex and Yves for pointing out what appear to be the only viable solutions. I think I'll probably go with the encoding idea rather than a structure.

  • Related