mikb55
August 21, 2023, 3:00pm
1
The documentation is a bit vague.
BitRead says "Reads a bit of a number." without mentioning if it only works for bytes or if other data types are supported.
Looking at past forum posts it seems that other people have had problems.
Is the safe way to assume that only bytes are properly supported and therefore you should use a union to make a big number appear as an array of bytes and work on those?
The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords.
You should use the real C++ bit manipulation operations instead of the Arduino macros (which are just crutches). If done properly, they can be applied to all integer types, However, beware shift operations on signed integer types.
https://www.geeksforgeeks.org/bitwise-operators-in-c-cpp/
https://www.tutorialspoint.com/bits-manipulation-important-tactics-in-cplusplus
J-M-L
August 21, 2023, 3:11pm
3
When you are not sure go check the source code
#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
#define bitSet(value, bit) ((value) |= (1UL << (bit)))
#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
#define bitToggle(value, bit) ((value) ^= (1UL << (bit)))
#define bitWrite(value, bit, bitvalue) ((bitvalue) ? bitSet(value, bit) : bitClear(value, bit))
So bitRead will work with everything up to unsigned long long (64 bits) as you just shift right and mask the LSb
but bitWrite uses bitSet which uses 1UL shifted left so you are limited at 32 bits. Had they used 1ull you would have been fine up to 64 bits.
1 Like
mikb55
August 21, 2023, 3:26pm
4
So should this be considered a bug?
It seem that both the documentation and the code have issues.
To me - there is no issue!
x: the number from which to read.
The maximum size (and hence the data type) of the number depends on the platform I am using.
Arduino UNO supports 64-bit data size. So, the permissible number is: 0x0000000000000000 - 0xFFFFFFFFFFFFFFFF.
The documentation doesn't even say integer instead of number .
Can you use those macros on a float or double ?
Sometimes the lack of whatever is lacking on those pages is frustrating.
a7
mikb55
August 21, 2023, 5:20pm
7
Read post #4 again. I specifically referenced bitWrite.
J-M-L
August 21, 2023, 5:22pm
8
Let’s call that a distinctive feature …
The documentation is full of artistic approximations
(Depending on how you fix the documentation then there might be no need to change the code)
Also newbies don’t know about ull suffix or uint64_t
1 Like
mikb55
August 21, 2023, 5:22pm
9
Exactly right. The target Arduino audience is 'beginners' and they have no idea what rules or special restrictions apply. As I mentioned at the start other people have found the documentation or behavior confusing.
If you are talking about floating point number , then the debate comes on.
I would say that I could --
Given:
float y1 = 15.73;
The value of 30th bit of the corresponding bit form is: 1
Serial.println(bitRead((*(long*)&y1, 30), BIN)); //shows: 1
This is from your post #1 .
J-M-L
August 21, 2023, 5:29pm
12
Look at the title of the thread
bitRead is there in the Title.
J-M-L
August 21, 2023, 5:31pm
14
He is saying he mentioned bitWrite, he did - and that is the one not supporting 64 bits, hence the bug comment
1 Like
To compensate it, there are so many supporting books, courses, online tutorials from third parties.
J-M-L
August 21, 2023, 5:34pm
16
As programmers say, the source code is the ultimate documentation…
GolamMostafa:
*(long*)&y1
This invokes undefined behaviour because of the strict aliasing rules.
1 Like
system
Closed
February 17, 2024, 6:47pm
18
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.