I'm having a weird issue with my code, basically, All im doing is Bit Masking. I create an object of type BitArray, and then I Set and unset bits. All of the Set/Unset operations work fine, however, they output numbers greater than one. This happens every time and they're always the same numbers.
void BitArray::Set(unsignedint index)
{
unsignedchar mask = (int)1;
double byte = (double)index/BITS_PER_BYTE;//
int position = index%BITS_PER_BYTE;
mask = mask << index;
barray[(int)byte] = (barray[(int)byte] | mask);
}
void BitArray::Unset(unsignedint index)//Not working
{
unsignedchar mask = (int)1;
double byte = (double)index/BITS_PER_BYTE;//
int position = index%BITS_PER_BYTE;
mask = mask << index;
barray[(int)byte] = (barray[(int)byte] ^ mask);
}
ostream& operator<< (ostream& os, const BitArray& a)//OR I can simply do a int to binary conversion
{
cout << "(";
for(int Ocount = 0; Ocount < a.Length()/BITS_PER_BYTE; Ocount++)
for(int Icount = 0; Icount < a.Length(); Icount++)
{
if(a.barray[Ocount] & ( 1 << Icount ))
os << "1";
else
os << (a.barray[Ocount] & ( 1 << Icount ));
}
cout << ")";
return os;
}
In the last function; operator<<, is where the magic happens! I did change the mask from int to character, but I don't think that has any effect because I cast it as an Int.
**Note: Although this issue has been fixed, there is still a bugg in the code that causes anything over 8 bits to be printed twice, investigating this now.
I would hardly call that a fix. Actually I know what is wrong: os << (a.barray[Ocount] & ( 1 << Icount ));
You need to shift the number back: os << ((a.barray[Ocount] & ( 1 << Icount )) >> Icount);