Reputation: 83
I am working on an arduino-like platform (very limited RAM), and I need to use a bit-field. I need to modify a specific bit in a byte, with something like this:
OneByte myByte = 0b11101111;
myByte[5] = 1;
To achieve this, I wrote the following:
typedef struct {
uint8_t bit0:1;
uint8_t bit1:1;
uint8_t bit2:1;
uint8_t bit3:1;
uint8_t bit4:1;
uint8_t bit5:1;
uint8_t bit6:1;
uint8_t bit7:1;
} BitByte;
typedef union {
BitByte asBits;
uint8_t asByte;
} BitField;
class OneByte
{
public:
OneByte(); // default constructor
~OneByte(); // delete
// Assignment operator
uint8_t& operator[] (const uint8_t pos );
uint8_t operator[] (const uint8_t pos ) const;
private:
BitField oneByte;
};
and in the .cpp I put
// I know a switch case is horrible, but don't want to think too much
// pos shoud always be between 0 and 7
uint8_t& OneByte::operator[] (const uint8_t pos )
{
if (pos < ByteSize)
{
switch (pos)
{
case 0:
return oneByte.asBits.bit0;
break;
case 1:
return oneByte.asBits.bit1;
break;
case 2:
return oneByte.asBits.bit2;
break;
case 3:
return oneByte.asBits.bit3;
break;
case 4:
return oneByte.asBits.bit4;
break;
case 5:
return oneByte.asBits.bit5;
break;
case 6:
return oneByte.asBits.bit6;
break;
case 7:
return oneByte.asBits.bit7;
break;
}
}
// If goes here, do some error handling
}
The uint8_t operator[] (const uint8_t pos ) const;
works fine, but the problem is with
uint8_t& operator[] (const uint8_t pos );
which fails to compile with the error:
error: cannot bind bitfield ‘((OneByte*)this)->OneByte::oneByte.BitField::asBits.BitByte::bit0’ to ‘uint8_t& {aka unsigned char&}’
I don't really know what to do in this case... Maybe make another class to wrap this one so I don't use the operator[]
overload?
Upvotes: 1
Views: 797
Reputation: 20063
As has already been noted you can't bind a bit-field to a non-const reference. Since you're targeting the Arduino using std::bitset
may not be a viable option depending on how much control you need over functionality or access to the data.
There are a couple of things i want to note though. First I don't recommend using C++ bit-fields for this. They're good if you need named access to the bits but in this case I think there are more maintainable ways to accomplish what you want. Second, returning uint8_t
or a reference to one from the index operators seems goofy, especially since you're actually working with boolean values.
Since you are working with individual bits you'll need to use a proxy object to sets and retrieve the individual bits without affecting all values in the bit-field. By providing a conversion operator and assignment operator for bool
types you can provide seamless access to the bit values. Something like the following should work for you.
#include <iostream>
#include <cstdint>
#include <string>
class Bits
{
typedef std::uint8_t value_type;
value_type bits;
struct Twiddler
{
Twiddler(value_type& value, size_t bitIndex)
: value(value), mask(1 << bitIndex)
{}
Twiddler& operator=(const Twiddler&) = delete;
Twiddler& operator=(bool bit)
{
value = value & ~mask | static_cast<value_type>(bit ? mask : 0);
return *this;
}
operator bool() { return (value & mask) != 0; }
private:
value_type& value;
value_type mask;
};
struct ConstTwiddler
{
ConstTwiddler(const value_type& value, size_t bitIndex)
: value(value), mask(1 << bitIndex)
{}
ConstTwiddler& operator=(const ConstTwiddler&) = delete;
operator bool() { return (value & mask) != 0; }
private:
const value_type& value;
value_type mask;
};
public:
Bits() : bits() {}
Bits(value_type bits) : bits(bits) {}
size_t size() const { return sizeof(bits) * 8; }
Twiddler operator[](size_t index)
{
if (index >= size())
{
throw std::out_of_range("Invalid bit index");
}
return Twiddler(bits, index);
}
const ConstTwiddler operator[](size_t index) const
{
if (index >= size())
{
throw std::out_of_range("Invalid bit index");
}
return ConstTwiddler(bits, index);
}
};
Upvotes: 2
Reputation: 302767
From [class.bit]:
A non-const reference shall not be bound to a bit-field (8.5.3).
So you simply cannot bind a uint8_t&
to any of your bits. You should consider instead using std::bitset
, which solves this problem by using proxy objects (std::bitset::reference
).
Upvotes: 1