4

I have a 5 byte data element and I need some help in figuring out how in C++ to set an individual bit of one of these byte; Please see my sample code below:

char m_TxBuf[4]; 

I would like to set bit 2 to high of byte m_TxBuf[1].

    
00000 0 00
      ^ This one

Any support is greatly appreciated; Thanks!

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
JB_SO
  • 471
  • 3
  • 8
  • 18
  • 1
    What? What does the 00** pattern mean? There are 14 digits in it, but you say you have 5 bytes of data (and you show a 4-byte array). And what do you mean by setting bit 2 to "high of byte m_TxBuf[1]"? – jalf Jun 28 '10 at 21:29
  • 1
    I believe he's just highlighting the bit he wants to set high. – Bill Lynch Jun 28 '10 at 21:38
  • Correct, just highlighting the bit to set, sorry for any confusion – JB_SO Jun 28 '10 at 21:44
  • and "high of byte" means? And why do you show a 4-byte array when you say you have 5 bytes of data? – jalf Jun 28 '10 at 21:51
  • 2
    it's not "high of byte", it's "bit 2 to high" "of byte m_TxBuf[1]". "high" is a word used in low-level digital electronics to mean the "on" state of a bit/wire/bus/etc, which in this context means "set it to 1" – rmeador Jun 28 '10 at 22:01
  • ooh, life would be a lot simpler if people used parentheses to eliminate ambiguity in english. :) – jalf Jun 28 '10 at 22:03

6 Answers6

14

Bitwise operators in C++.

"...set bit 2..."

Bit endianness.

I would like to set bit 2 to high of byte m_TxBuf[1];

m_TxBuf[1] |= 1 << 2

Andras Vass
  • 11,478
  • 1
  • 37
  • 49
4

You can use bitwise-or (|) to set individual bits, and bitwise-and (&) to clear them.

Anon.
  • 58,739
  • 8
  • 81
  • 86
3
m_TxBuf[1] |= 4;

To set a bit, you use bitwise or. The above uses compound assignment, which means the left side is one of the inputs and the output.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
3
int bitPos = 2;  // bit position to set
m_TxBuf[1] |= (1 << bitPos);
Amardeep AC9MF
  • 18,464
  • 5
  • 40
  • 50
2

Typically we set bits using bitwise operator OR (operator| or operator|= as a shorthand).

Assuming 8-bits to a byte (where the MSB is considered the '7st' bit and the LSB considered the 0th: MSB 0) for simplicity:

char some_char = 0;
some_char |= 1 << 0; // set the 7th bit (least significant bit)
some_char |= 1 << 1; // set the 6th bit
some_char |= 1 << 2; // set the 5th bit
// etc.

We can write a simple function:

void set_bit(char& ch, unsigned int pos)
{
    ch |= 1 << pos;
}

We can likewise test bits using operator&.

// If the 5th bit is set...
if (some_char & 1 << 2)
    ...

You should also consider std::bitset for this purpose which will make your life easier.

stinky472
  • 6,737
  • 28
  • 27
  • When you mean the 8th bit, do you mean for the value of 1 or 128 (128-000000-1) – JB_SO Jun 28 '10 at 21:38
  • @JB_SO Apologies, I clarified that confusing part in the post. I'm going left to right from MSB (most significant bit) to LSB as we typically represent bits on paper. The MSB is the 1st bit, LSB 8th. – stinky472 Jun 28 '10 at 21:47
  • Setting the 8th bit, therefore, increases the value by 1. – stinky472 Jun 28 '10 at 21:49
  • I switched it to MSB 0 convention (ranging from 0 to 7) since '8th' bit really is confusing in retrospect. – stinky472 Jun 28 '10 at 22:09
0

Just use std::bitset<40> and then index bits directly.

Edward Strange
  • 40,307
  • 7
  • 73
  • 125