0

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

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
isme
  • 13
  • 1
  • 2
  • 2
    6 bytes is 48 bits, so you could use `int64_t` which is 64 bits (the clue is in the name). Is there anything you want from a 48 bit integer that you wouldn't get from a 64 bit integer? Clariication of exacly what you want and why is needed. – john Jul 15 '20 at 06:37
  • 2
    Don't forget you need to `#include ` for `int64_t`. – john Jul 15 '20 at 06:40
  • i want it because of curiousness. i think that if i can use 6byte data type in c, i can also use bigger than long long int. – isme Jul 15 '20 at 06:43
  • Is this about signed or unsigned integer? Please, note that [Two's complement](https://en.wikipedia.org/wiki/Two%27s_complement#:~:text=Two's%20complement%20is%20the%20most,the%20inverse%3A%20%E2%88%92210.) is very common nowadays. The convenient fact for you: -1 in `int64_t` is stored with all bits set. If you mask away the upper 2 bytes to get a 6 byte integer, it still has all bits set and still means -1. (Accordingly for all the other values - ignoring overflow.) – Scheff's Cat Jul 15 '20 at 06:44
  • 1
    [If a 32-bit integer overflows, can we use a 40-bit structure instead of a 64-bit long one?](https://stackoverflow.com/q/27705409/995714), [Which C datatype can represent a 40-bit binary number?](https://stackoverflow.com/q/9595225/995714), [Is there a highly performant way to make a 56 bit integer?](https://stackoverflow.com/q/50780920/995714), [12bit variable, how can I do this?](https://stackoverflow.com/q/43152077/995714) – phuclv Jul 15 '20 at 06:58
  • 1
    Does this answer your question? [Which C datatype can represent a 40-bit binary number?](https://stackoverflow.com/questions/9595225/which-c-datatype-can-represent-a-40-bit-binary-number) – phuclv Jul 15 '20 at 06:58
  • 1
    Well, you'd have to switch to another compiler and your code won't be portable... but clang recently added support for integers with arbitrary width (anything from 1 to 16777215 bit). This [_ExtInt](http://blog.llvm.org/2020/04/the-new-clang-extint-feature-provides.html) feature has also been proposed to the C committee, so it might one day be incorporated into the standard. – Felix G Jul 15 '20 at 07:11
  • @양사Yangsa simply use bitfields - the compiler will do the rest for you. – 0___________ Jul 15 '20 at 08:21

2 Answers2

2

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

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
2

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.

pholat
  • 467
  • 5
  • 20
  • Bit field ability with `uint64_t` relies on the implementation - not fully portable. "A bit-field shall have a type that is a qualified or unqualified version of `_Bool, signed int, unsigned int`, or some other implementation-defined type." – chux - Reinstate Monica Jul 16 '20 at 02:44