0

I'm wondering if one needs to assign a bit-stream of 78 bits to a variable(later on to be processed), what datatype can be used? What is the max bit can be stored into a variable in C?

  • How about an array ob bytes? also 78 is not divisible by 8. no matter what type you take, you will have fractional parts. – Kwasmich Nov 18 '19 at 16:10
  • But I can still store 78 into anything multiple of 8. Its not a problem rest of the bits can be zero. – floppy380 Nov 18 '19 at 16:19
  • Would in 64 can store 64 bits? How about if more than 64 and less than 128 bits are to assigned? Is it impossible? – floppy380 Nov 18 '19 at 16:21
  • FWIW, sizeof(intmax_t) == 8, at least on the AVR-based boards. – Edgar Bonet Nov 18 '19 at 18:00
  • Here is a template class that handles this, github.com/mikaelpatel/Cosa/blob/master/cores/cosa/Cosa/…. – Mikael Patel Nov 18 '19 at 18:52
1

There is no builtin data type that can store 78 bits. However, it depends a bit what you want to store. Like the following cases:

  • If the 78 bits are one 'entity' (e.g. one really big value), you can store it as an array of bytes (10 bytes of 8 bits = 80 bits, leaving 2 bits unused)
  • If the 78 bits can be split up in smaller parts, you can:
    • Create a structure or class to contain the sub elements into fields (instance variables), using for each sub type a normal type (e.g. signed/unsigned 8, 16 or 32 bits).
    • Create bit fields to save some memory if there are small items and memory space is an issue.
    • Or a combination of the above

If you have many of these items and you want to save space (thus the 2 unused bits of each 80 bits), you can create a big array that contains many items, and use bit operations to get each item. E.g. the first element will be in the first 10 bytes, where the last 2 bits are not part of the element. The second item will use the remaining 2 bits and the following 78 - 2 = 76 bits, etc. This saves only 2/80 * 100% = 2.5% so only use this when really needed.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.