Solved

Setting the bits in an unsigned char.

Posted on 2007-12-02
20
195 Views
Last Modified: 2010-04-21
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;

}

Open in new window

0
Comment
Question by:Unimatrix_001
  • 8
  • 8
  • 3
20 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 250 total points
ID: 20392770
Something like this :


void setBit(BYTE &aByte, int pos) {

    aByte |= 0x01 << pos;

}

Open in new window

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20392776
And a nice getBit :


bool getBit(BYTE aByte, int pos) {

    return (aByte & (0x01 << pos));

}

Open in new window

0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 20392777
Actually, also how do I clear a bit?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20392780
>> And a nice getBit :

With the code :

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

Expert Comment

by:Jaime Olivares
ID: 20392783
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;

}

Open in new window

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20392790
BYTE convertBitsToByte(BIT *bits)
{
      result = 0;
      for(int bitCount=7;bitCount>=0;bitCount--)
          result = (result<<1) | bits[i];
      return result;
}
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20392794
>> Actually, also how do I clear a bit?

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

Author Comment

by:Unimatrix_001
ID: 20392796
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 20392816
>> 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Author Comment

by:Unimatrix_001
ID: 20392823
>>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
 
LVL 53

Expert Comment

by:Infinity08
ID: 20392850
>> 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
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 20392867
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 20392873
>> 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
 
LVL 53

Expert Comment

by:Infinity08
ID: 20392874
Do take a look at bitset in that case ...
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 20392876
I shall... (-:
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 20393064
Jamie, can you please post a comment...
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 250 total points
ID: 20393080
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
 
LVL 3

Author Closing Comment

by:Unimatrix_001
ID: 31412234
Yes, something just like that. :)
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 20393090
Thank you. :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now