Reputation: 13
I want to use 6 byte data type in c, but I cannot found how to do. Can you teach me how to use 6 byte integer data type in c?
I use dev c++, and codeblocks.
I want to use c, but i can also use c++. Windows 64-bit
Upvotes: 0
Views: 5425
Reputation: 1
Read the C11 standard n1570 and the C++11 standard n3337.
Refer also to this C and C++ reference website.
Read also good C programming books (Modern C) and a good C++ programming book (and the documentation of your compiler).
(except theoretically on weird C or C++ implementations; I cannot name any existing one in 2020)
You could use bit fields in struct
or (instead) 64 bit numbers std::int64_t
) combined with bitwise operations (e.g. bitwise and &
or bitwise or |
or bitwise not ~
or bit shifts operations <<
or >>
)
You might generate serialization routines, e.g. use SWIG. Beware of endianness issues. For ease of debugging, you might prefer textual formats like JSON instead of sending binary packets.
A typical example of 48 bits numbers are Ethernet frame MAC addresses. See example code mentioned from OSDEV wiki.
Upvotes: 2
Reputation: 487
There is a way to have exact number of bits for something in C, so in theory it's doable, but it will take more than 48 bits in memory.
To do so you can use bit fields from C, to do so you can:
struct your_type {
uint64_t your_value : 48;
};
With this you can create such struct, and access your_value which will have 48 bit representation. It will be treated as uint64_t under the hood.
Saying that I strongly recommend reading Ansi C
or any other basic C book to get to know basics better.
Please mind that creating parsers with bitfields is generally bad idea due to padding, packing and endianess issues issues. If you need to have any protocol please take interest in messagepack or any other protocol library.
Upvotes: 2