adolfintel / OpenPods

The Free and Open Source app for monitoring your AirPods on Android
https://fdossena.com/?p=openPods/index.frag
GNU General Public License v3.0
932 stars 161 forks source link

[Question] isFlipped function #100

Closed steam3d closed 3 years ago

steam3d commented 3 years ago

Why did you sum 0x10 to char?

private boolean isFlipped (String str) {
    return (Integer.toString(Integer.parseInt("" + str.charAt(10), 16) + 0x10, 2)).charAt(3) == '0';

For example char is 2

Integer.toString(Integer.parseInt("" + '2', 16), 2)) // is 10
Integer.toString(Integer.parseInt("" + '2', 16) + 0x10, 2)) // is 10010

It does not matter if you take char(3) from 10010 or take char(0) from 10

adolfintel commented 3 years ago

You're right. It must be a leftover from some test that I was doing, I'll remove it.

Electric1447 commented 3 years ago

but if char is 1 or 5 the result is different

Electric1447 commented 3 years ago

You can see that it is not the same: the left result is the original

0:  0 - 0
1:  0 - 1
2:  1 - 1
3:  1 - 1
4:  0 - 1
5:  0 - 1
6:  1 - 1
7:  1 - 1
8:  0 - 1
9:  0 - 1
A:  1 - 1
B:  1 - 1
C:  0 - 1
D:  0 - 1
E:  1 - 1
F:  1 - 1
0:  10000 - 0
1:  10001 - 1
2:  10010 - 10
3:  10011 - 11
4:  10100 - 100
5:  10101 - 101
6:  10110 - 110
7:  10111 - 111
8:  11000 - 1000
9:  11001 - 1001
A:  11010 - 1010
B:  11011 - 1011
C:  11100 - 1100
D:  11101 - 1101
E:  11110 - 1110
F:  11111 - 1111
steam3d commented 3 years ago

You are right, I forget write that you need to fill zeros to xxxx format. I think this is wrong just add 0x10 to char. It is confusing.

Electric1447 commented 3 years ago
private boolean isFlipped (String str) {
    return ((Integer.parseInt("" + str.charAt(10), 16) / 2 ) % 2) == 0;
}

This would probably be a better idea

adolfintel commented 3 years ago

This could be rewritten better as

return (Integer.parseInt(""+str.charAt(10),16) & 0x02)!=0;

since we're just interested to the value of a single bit.

I have no idea what I was thinking when I wrote that last year.

Electric1447 commented 3 years ago

Do you mean this?

return (Integer.parseInt("" + str.charAt(10), 16)  &  0x02) == 0;
adolfintel commented 3 years ago

Yes, my bad. I'm fucking retarded today.

Electric1447 commented 3 years ago

done that in ea1743a