C/C++ Bit Array or Bit Vector

前端 未结 5 1311
北荒
北荒 2020-12-05 12:29

I am learning C/C++ programming & have encountered the usage of \'Bit arrays\' or \'Bit Vectors\'. Am not able to understand their purpose? here are my doubts -

5条回答
  •  悲&欢浪女
    2020-12-05 12:50

    Bit Arrays or Bit Vectors can be though as an array of boolean values. Normally a boolean variable needs at least one byte storage, but in a bit array/vector only one bit is needed. This gets handy if you have lots of such data so you save memory at large.

    Another usage is if you have numbers which do not exactly fit in standard variables which are 8,16,32 or 64 bit in size. You could this way store into a bit vector of 16 bit a number which consists of 4 bit, one that is 2 bit and one that is 10 bits in size. Normally you would have to use 3 variables with sizes of 8,8 and 16 bit, so you only have 50% of storage wasted.

    But all these uses are very rarely used in business aplications, the come to use often when interfacing drivers through pinvoke/interop functions and doing low level programming.

提交回复
热议问题