C + + implementation bit array

Source: Internet
Author: User

When we encounter a lot of integer sorting to save memory space we can consider using a bit array to implement, the disadvantage is that it only applies to positive integers.

Thought:

In a 32-bit system with an int type of 4 bytes, a bitwise to calculate an int type can record 32 numbers, therefore, an int array and a shift are used to implement the related function.

C + + implementation bit array

#include <iostream>using namespace std;const unsigned int bitvalue[32]={0x80000000,0x40000000,0x20000000, 0x10000000,0x08000000,0x04000000,0x02000000,0x01000000,0x00800000,0x00400000,0x00200000,0x00100000,0x00080000, 0X00040000,0X00020000,0X00010000,0X00008000,0X00004000,0X00002000,0X00001000,0X00000800,0X00000400,0X00000200, 0x00000100,0x00000080,0x00000040,0x00000020,0x00000010,0x00000008,0x00000004,0x00000002,0x00000001};const int Bitlen =sizeof (int) *8;class bitarray{private:unsigned int len;unsigned int *bit;public:bitarray (unsigned int length) { if (length<0) {throw "length less than 0";} this->len=length;bit= New unsigned int[length/bitlen+ (length%bitlen>0?1:0)] ();//initialized to 0}unsigned int getbit (int Index) {if (index<0| | Index>len) {Throw "index out of Bounds";} unsigned int data=bit[index/bitlen];return (Data&bitvalue[index%bitlen]) >> (bitlen-index%bitlen-1);} void setbit (unsigned int index,unsigned int value) {if (index<0| | Index>len) {Throw "index out of Bounds";} if (value!=1&&value!=0{Throw ' value can only be 1 or 0 ";} unsigned int data=bit[index/bitlen];//calculates which int value in the array if (value==1) {Bit[index/bitlen]=data|bitvalue[index%bitlen];} Else{bit[index/bitlen]=data&~bitvalue[index%bitlen];}} unsigned int getlength () {return this->len;} ~bitarray () {delete[] bit;}; int main () {Try{bitarray barray (1000000); Barray.setbit (99999,1); Barray.setbit (201,1); cout<<barray.getbit (0) <<endl;cout<<barray.getbit (99999) <<endl;cout<<barray.getbit (10000) <<endl;} catch (char *str_ex) {Cout<<str_ex<<endl;} Cin.get (); return 0;}


Copyright NOTICE: Welcome to reprint, if there are deficiencies, please treatise.

C + + implementation bit array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.