How to print bits of an int variable?

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?


void setup()
{
   Serial.begin(9600);
   Serial.println();
   Serial.println(75);
   Serial.println(75, BIN);
   Serial.println('A');
   Serial.println('A',BIN);
   Serial.println(1.912,BIN);
}
void loop()
 {
  }

Good luck with that one.

Write your own function that takes a uint32_t and prints the bits, using bitRead and a for loop.

The built-in BIN modifier omits leading zeroes, and may confuse more than it illuminates.

Just making sure I understood correctly. The want is an array of 2000 bits, correct?

bool AthingOf2000bits[2000] ;

You might need a bigger box but possible.

A bool is not a bit.

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.

You can easily implement your own array of bits, e.g.

2000 bits!!!

2^2000 = 1.148130695 E+602

The universe is vast. Scientists estimate there are 10^80 atoms in the universe.

2000 bits will allow you to represent numbers far beyond the number of atoms in the Universe. I suggest you don't need 2000 bits.

Or maybe I'm missing the point.

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()
{
}

Yes :slightly_smiling_face:
I'll be using the bits as 'markers' to indicate whether a track has been played or not. For more backgound see posts #5, #6 and #10 of thread:
https://forum.arduino.cc/t/library-with-feature-like-excel-s-unique-function/1088991

1 Like

No, not an aray. "UNO memory capacity is insufficient with simpler array methods."
See also post #8.

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.

Thanks, but way over my head! I have little C/C++ skill.

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.

Thanks, got it.

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?

OK, crossed in the ether. I'll pursue that, thanks.

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)

1 Like

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.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.