Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set individual bit in C++

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!

like image 244
JB_SO Avatar asked Nov 27 '22 23:11

JB_SO


1 Answers

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

like image 199
Andras Vass Avatar answered Dec 04 '22 22:12

Andras Vass