Bit Masking

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.

Functions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
BitArray::BitArray(unsigned int a)//Constructor, takes an INT in for how many //bits to create(minimum_)
{
	double size = sizeof(unsigned char);
	size = size*BITS_PER_BYTE;
	double bytes = a/size;
	arraySize = ceil(bytes);

	barray = new unsigned char[arraySize];
	for(int i = 0; i < arraySize;i++)
	{
		barray[i] = (int)0;//Everything should be Zero now
	}
}
void BitArray::Set(unsigned int index)
{
	char mask = 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(unsigned int index)//Not working
{
	int mask = 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++)
		{
			os << (a.barray[Ocount] & 1 << Icount);
		}
	cout << ")";
		return os;
}


If I run:
1
2
3
4
5
6
7
8
9
        BitArray ba(8);//8 bits, 1 byte
	cout << ba;
	cout << endl;
	ba.Set(2);
	cout << "After Set: \n";
	cout << ba;
	ba.Unset(2);
        cout << "\nAfter unSet: \n";
	cout << ba;

The output looks like this:

(00000000)
After Set:
(00400000)
Adter unSet:
(00000000)


Where as I'm trying to get the "4" and any other bits that are set, to print as "1". Any Advice?



os << (a.barray[Ocount] & 1 << Icount);
This prints 4 because operator precedence is undefined for binary operations.

Try:
os << (a.barray[Ocount] & ( 1 << Icount ));

Edit: the number four is 1 << 2
Last edited on
This actually made no change in the output :(
FIGURED IT OUT!!! For any future people:
If it's not included, then I didn't make any changes to it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
void BitArray::Set(unsigned int index)
{
	unsigned char 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(unsigned int index)//Not working
{
	unsigned char 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.
Last edited on
**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);
I can't see any point in using double types for your variables - why did you do that? What is wrong with int?
Topic archived. No new replies allowed.