Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i define 24 bit array in c++?

how do i define 24 bit array in c++? (variable declaration)

like image 527
zoi Avatar asked Dec 03 '22 11:12

zoi


2 Answers

There is no 24-bit variable type in C++.

You can use a bitpacked struct:

struct ThreeBytes {
    uint32_t value:24;
};

But it is not guaranteed that sizeof ThreeBytes == 3.

You can also just use uint32_t or sint32_t, depending on what you need.

Another choice is to use std::bitset:

typedef std::bitset<24> ThreeBytes;

Then make an array out of that:

ThreeBytes *myArray = new ThreeBytes[10];

Of course, if you really just need "three bytes", you can make an array of arrays:

typedef uint8_t ThreeBytes[3];

Note that uint8_t and friends are non-standard, and are used simply for clarification.

like image 66
strager Avatar answered Feb 22 '23 08:02

strager


An unsigned byte array of 3 bytes is 24 bits. Depending on how you are planning to use it, it could do.

unsigned char arrayname[3]

As @GMan points out you should be aware that it's not 100% of all systems that has 8 bits chars.

like image 43
Jonas Elfström Avatar answered Feb 22 '23 08:02

Jonas Elfström