简体   繁体   中英

How can i use 6 byte integer data type in C?

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

Read the C11 standard n1570 and the C++11 standard n3337 .

C & C++ are different programming languages

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).

6 bytes integers are not mentioned there.

(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 (eg bitwise and & or bitwise or | or bitwise not ~ or bit shifts operations << or >> )

You might generate serialization routines, eg 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM