• Status: Solved
• Priority: Medium
• Security: Public
• Views: 211

# Setting the bits in an unsigned char.

Hi,

I use the attached method to get the bits out of a byte, but what is the equivalent of setting the bits in a byte?

Thanks,
Uni
``````BIT* convertByteToBits(BYTE aByte){
BIT *aBits=new BIT[8];
for(unsigned int bitCount=0;bitCount<8;bitCount++)
aBits[bitCount]=((aByte>>bitCount)&1);
return aBits;
}
``````
0
Unimatrix_001
• 8
• 8
• 3
2 Solutions

Commented:
Something like this :

``````void setBit(BYTE &aByte, int pos) {
aByte |= 0x01 << pos;
}
``````
0

Commented:
And a nice getBit :

``````bool getBit(BYTE aByte, int pos) {
return (aByte & (0x01 << pos));
}
``````
0

Author Commented:
Actually, also how do I clear a bit?
0

Commented:
>> And a nice getBit :

With the code :

bool getBit(BYTE aByte, int pos) {
return (aByte & (0x01 << pos));
}
0

Software ArchitectCommented:
I should be something like:

``````BYTE convertBitsToBytes(BIT *bits)
{
BYTE result = 0;
for (int bitCount=7;bitCount>=0;bitCount--)
result = (result<<1) | bits[i];
return result;
}
``````
0

Software ArchitectCommented:
BYTE convertBitsToByte(BIT *bits)
{
result = 0;
for(int bitCount=7;bitCount>=0;bitCount--)
result = (result<<1) | bits[i];
return result;
}
0

Commented:
>> Actually, also how do I clear a bit?

void clearBit(BYTE &aByte, int pos) {
aByte &= (0xFF ^ (0x01 << pos));
}
0

Author Commented:
Okay, cheers for that Jamie... Sorry Infinity, I'm going to get the question reopened to split the points 50/50 with you and Jamie... My bad though, I shouldn't have worded the question as setting the bits...
0

Commented:
>> My bad though, I shouldn't have worded the question as setting the bits...

It's quite inefficient to do it that way though - that's why I posted code to set and get single bits. For most applications, there's no need to convert a byte to an array of bits - you can simply loop over the bits in the byte directly.

You can also consider the std::bitset :

http://www.cplusplus.com/reference/stl/bitset/

but it's not much better.
0

Author Commented:
>>For most applications, there's no need to convert a byte to an array of bits - you can simply loop over the bits in the byte directly.

I fully plan to do that, although that makes the works slightly harder, so I'll leave that for a future time. ;)
0

Commented:
>> I fully plan to do that, although that makes the works slightly harder, so I'll leave that for a future time. ;)

It doesn't make it harder. Instead of this :

BIT *bits = convertByteToBits(aByte);
for (int i = 0; i < 8; ++i) {
std::cout << bits[i];
}
delete [] bits;

you'd simply have :

for (int i = 0; i < 8; ++i) {
std::cout << getBit(aByte, i);
}

I don't see how that's harder ... You can even bypass the function call if you feel like it :

for (int i = 0; i < 8; ++i) {
std::cout << ((aByte >> i) & 0x01);
}
0

Author Commented:
Well you see I dot about the file for certain bits, so at the moment it's easier for me to just read all the file in as bits...
0

Commented:
>> Well you see I dot about the file for certain bits, so at the moment it's easier for me to just read all the file in as bits...

I disagree, but it's your code ;)
0

Commented:
Do take a look at bitset in that case ...
0

Author Commented:
I shall... (-:
0

Author Commented:
0

Software ArchitectCommented:
My solution:
BYTE convertBitsToByte(BIT *bits)
{
result = 0;
for(int bitCount=7;bitCount>=0;bitCount--)
result = (result<<1) | bits[i];
return result;
}

Greetings,
Jaime.
0

Author Commented:
Yes, something just like that. :)
0

Author Commented:
Thank you. :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.