Here’s the thing : I have a problem to get some bit in a binary number, for example :
8 Bit binary code : “01234567” (I mean 01010101, just like that !)
I want to get 4 bit from least significant bit (right most) : 4567, then convert them into decimal number
How can I do that ?
void setup() {
Serial.begin(9600);
delay(5000);
byte b = 0b01010101;
// Get the 4 least-signficant bits by logically ANDing with 0x0f which is 0b00001111
int val = (int)(b & 0x0f);
// If you want a String as a decimal mumber, you can do this:
String str = String(val, DEC);
Serial.printlnf("input=%d val=%d string=%s", (int)b, val, str.c_str());
}
void loop() {
}
When I try your code, with a gaugingStatus value that has the twos bit set (50 for instance), I get FullCharged = 1, which is what you should get. What values did you test with?
Weird, normally it worked for me also and it is working for other variables but it’s sporadic for some reason. I don’t think I’m out of memory or anything.
I was basically testing against the same variable and getting different results. I know the variable was correct because I was serial printing it out to make sure it was not changing for any reason.
I’ve switched to using the bitRead function now in the if statements and everything is working now without changing anything else which is strange.
It’s like 1500 lines of code so kinda too much to share here but I posted the main function that was not returning the bit status properly. I’m guessing something else is up, the size of the sketch may have something to do with it but I’m not sure yet.
That seems a little troubling in itself, since the first method and bitRead are doing very similar things. It's hard to see why one would work and not the other. I'd be a little worried about what else might be going wrong (or could go wrong) in your program with this kind of strange behavior. Have you tried using both methods, one right after the other in your code to see what that shows you (and maybe try reversing which method comes first in the code to see if that makes a difference)?
Yes, I did try both methods at the same time and got different results with bitRead returning the correct values.
The Variable I was reading from I serial printed and it was always correct.
The weird part was that the first function was working for some bits but not displaying others set to ON from the Same Variable. Using bitRead worked for all of them though.
It’s working now so I’m not going to spend much time on it unless other issues pop up.
I find using bitRead more friendly because I can just enter the bit number I’m wanting to read vs counting out how many zeros go before the bit I want to read using the 0b00100000 method.
Everything else is and has been working perfectly fine.
Is there any way to get the Photon’s remaining memory and data space left once a sketch is loaded when using the Desktop IDE software? Or do I need to compile this folder via the CLI to get that data?