C++ Program to Implement Bit Array

This C++ Program demonstrates the implementation of Bit Array.

Here is source code of the C++ Program to demonstrate the implementation of Bit Array. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to Implement Bit Array
  3.  */
  4. #include <iostream>
  5. #include <string>
  6. using namespace std;
  7. #define BIT_ARRAY_LENGTH 4
  8.  
  9. typedef struct 
  10. {
  11.     unsigned int bit : 1;
  12. } Bit;
  13.  
  14. /*
  15.  * Bit Array Class 
  16.  */
  17. class BitArray
  18. {
  19.     private:
  20.         Bit **bitValues;
  21.     public:
  22.         BitArray()
  23.         {
  24.             bitValues =  new Bit* [BIT_ARRAY_LENGTH];
  25.         }
  26.         /*
  27.          * Return Bit at a position
  28.          */
  29.         Bit *getBit(int value,int position)
  30.         {
  31.             Bit *singleBit = new Bit;
  32.             singleBit->bit = 0;
  33.             if(position == 0) 
  34.             {
  35.                 singleBit->bit = value & 1;
  36.             }
  37.             else 
  38.             {
  39.                 singleBit->bit = ( value & (1 << position ) ) >> position;
  40.             }
  41.             return singleBit;
  42.         }
  43.         /*
  44.          * Set Bit at a position
  45.          */
  46.         BitArray *setBit(BitArray *bt,Bit *bit,int position)
  47.         {
  48.             bt->bitValues[position] = bit;
  49.             return bt;
  50.         }
  51.         /*
  52.          * Return value of a bit array
  53.          */
  54.         int getValue(BitArray *bitArray)
  55.         {
  56.             int value = 0;
  57.             unsigned int bitValue = 0;
  58.             bitValue = bitArray->bitValues[0]->bit;
  59.             value |= bitValue;
  60.             for(int i = 1; i < BIT_ARRAY_LENGTH; i++)
  61.             {
  62.                 bitValue = bitArray->bitValues[i]->bit;
  63.                 bitValue <<= i;
  64.                 value |= bitValue;
  65.             }
  66.             return value; 
  67.         } 
  68. };
  69. /*
  70.  * Main
  71.  */
  72. int main()
  73. {
  74.     int val;
  75.     cout<<"Enter 4 bit integer value(0 - 8): ";
  76.     cin>>val;
  77.     BitArray bt, *samplebt;
  78.     samplebt = new BitArray;
  79.     for (int i = 0; i < BIT_ARRAY_LENGTH; i++)
  80.     {
  81.         samplebt = bt.setBit(samplebt, bt.getBit(val, i), i);
  82.         cout<<"Bit of "<<val<<" at positon "<<i<<": "<<bt.getBit(val, i)->bit<<endl;
  83.     }
  84.     cout<<"The value is: "<<bt.getValue(samplebt)<<endl;
  85.     return 0;
  86. }

$ g++ bit_array.cpp
$ a.out
 
Enter 4 bit integer value(0 - 8): 7
Bit of 7 at positon 0: 1
Bit of 7 at positon 1: 1
Bit of 7 at positon 2: 1
Bit of 7 at positon 3: 0
The value is: 7
 
 
------------------
(program exited with code: 1)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.