Get bit position

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 ? :smile:

Thank you guys, sorry for my bad English

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

Do you have a sample of the code that stores the 8bit binary number? what data format is it stored in?

I imagine something like:

uint8_t data = 0B11110110;
uint8_t mask = 0B11110000;
uint8_t filteredData = data & mask;

wow @rickkas7, fast :wink:

2 Likes

Thank you guys so much. One quick question with the same issue above : “01234567”, how can I get only one specific bit like number “4” or “2” ?

You can use a 0b constant by inserting a 1 in the bit you want, so: here’s your bit number 4:

  01234567
0b00001000

If you want it in a test, you’d have something like:

if (value & 0b00001000) {
}
1 Like

If you want this dynamic try this

byte x = someNumber;
int pos = 4;
bool bitIsSet = (x & (1 << pos)); // 0b00000001 becomes 0b00010000

Don’t forget bitRead()

bitRead()

Description
Reads a bit of a number.

Syntax
bitRead(x, n)

Parameters
x: the number from which to read

n: which bit to read, starting at 0 for the least-significant (rightmost) bit

Returns
the value of the bit (0 or 1).

:wink:

@BulldogLowell, what header would need to be included for this?

It’s common on Arduino, but not there by default on Particles.

Rats!

I thought it was standard in Wiring!

Thank God you caught that.

1 Like

BTW, even on Arduino it’s nothing else than a macro using the bit shift I’ve done above.
Just the other way round :wink:

#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
2 Likes

Amazing stuff, thanks!

Good to know!

@rickkas7 Thanks for the tip above, it was exactly what I needed and it’s so simple :smiley:

1 Like

Following up with this thread due to weird behavior.

So to keep this short I have been using this with good success to read bit 1 or 0 status on 16 and 32 bit global variables.

if (gaugingStatus & 0b00000010) {FullCharged = 1;}
else {FullCharged = 0;}

Now for some reason the above if statment returns False when it’s really true.

I just started using the bitRead function I found on the forum as seen below as a test :

if (bitRead(gaugingStatus, 1)) {Serial.print("Fully Charged = True");}

It returns True which is correct.

Any ideas on why the first method returns false while the second returns true when reading from the same unchanged variable?

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?

It was HEX 4A

I was wanting to know if bit position 1 was 1, and it was but the first method returns false for me while the 2nd method returns true.

When I test with 0x4a, the first method returns true (that is FullCharged = 1). Can you show the exact code you’re using to test?

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. :face_with_raised_eyebrow:

Just glad it’s working now using bitRead.

Thanks for trying to troubleshoot with me @Ric

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?