I’m learning about bit processing. The context is that I want to use a 2,000 bit int variable as a way of ensuring unique playing of an MP3 player. UNO memory capacity is insufficient with simpler array methods.
The example code below shows only 32 bits. How would I display say 100 which would be about the minimum I think I’ll need for thorough testing?
My recollection of C/C++ is that you can't have an array of a "thing"if you can't take its address, but you can easily emulate an array of bits with a class.
Thanks, I'd be happy if that was the limitation. But the tests I made with the following code appear to confirm that there's an (undocumented?) limit of 32 bits. As you see,
2^32 - 1
printed OK, just within the limit.
//unsigned long value = 8589934592; // Fails, 0
//unsigned long value = 4294967296; // 2^32 Fails. 0
//unsigned long value = 85899; // OK, 10100111110001011
unsigned long value = 4294967295; // 2^32 - 1, OK, 32 bits
// all set to 1
void setup()
{
Serial.begin(115200);
// Serial.print();
Serial.print("value as binary = ");
Serial.println(value, BIN);
}
void loop()
{
}
That's the size of your variable - "unsigned long" (aka uint32_t) is 32 bits.
There's uint64_t, but Arduino support for these is limited.
However, your array of bits will be implemented as 250 bytes.
Very simply, take an array of 250 uint8_t, and use the / and % operators to allow you to isolate bits.
Use bitRead if your bit operations are not up to snuff.
OK, I think I follow. Does that mean that I can read and write bits upstream of the 32nd, although I won't have the convenience of actually seeing them?
I don't understand what "upstream" means in this context.
Example: say I want to reset bit 193.
This bit will be contained in byte 193 / 8, and in bit 193 % 8 of that byte.
A simple bitWrite can be used to reset that bit (or set it)
OK, understood. I meant I'd never see all the bits together if there were more than 32 of them. 'Upstream' of bit #31 counting leftwards from zero. (Or bit #32 from 1, which is where I intuitively start counting! ) But you've confirmed I can operate on them, thanks.