PDA

View Full Version : access to bits of a char variable



Alex22
27th December 2015, 16:57
hi, there is a char variable. i need to access to the bits of that. i think i can do this by QBitArray class. please help me in this way. i searched this but saw no example.
thanks for any help

anda_skoa
27th December 2015, 17:47
You can also just use the bit maipulation operators, such as & and |

Cheers,
_

Alex22
27th December 2015, 18:47
You can also just use the bit maipulation operators, such as & and |

Cheers,
_

thanks, can you give an example how i can access to bits of an unsigned char variables by QBitArray class? is this code true?


unsigned char un;
QBitArray bits(8);
bits = (QBitArray) un;


thanks fore more help

ChrisW67
27th December 2015, 20:49
QBitArray is a device for managing an arbitrary length list of single-bit flags, not general bit manipulations. There is no obvious way to construct a QBitArray from a preexisting integer that does not involve solving the bit access problem manually first.



char c = 0xaa; // 1010 1010
for (int i = 0; i < 8; ++i)
qDebug() << "Bit" << i << ":" << ((c >> i) & 0x01);

char bit34 = (c & 0x18); // you may want to right shift these >> 3
// clear bits 3 and 4
c = (c & ~0x18);
// set bits 3 and 4
c = (c | 0x18);