kavala
March 27, 2024, 10:22pm
1
What is the syntax for this instruction.
bit(n) of what ?
There must be another argument. I've tried several formats. None worked.
There don't seem to be any examples.
Obviously, there are other ways to do this, but I was curious as to the correct
syntax ?
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
long x;
for (int n = 0; n < 16; n++) {
x = bit(n); // equivalent to x = 1 << n;
Serial.println(x, BIN);
}
}
void loop() {
It returns the value of the bit 2^n where bit n is the specified bit. For example bit(3) = 2^3 or 8. What did you think it did?
Maybe you are thinking it is one of these
bitClear()
bitRead()
bitSet()
which do take two arguments, something to operate upon, and a bit number.
Note that the result is the returned value, the argument is not changed.
HTH
a7
kavala
March 27, 2024, 11:09pm
6
I know what it does....That wasn't my question...
kavala
March 27, 2024, 11:29pm
8
Yes, but there is an argument missing.
Bit 'n' of what BYT E is my question
bit(n) only takes one argument.
kavala
March 27, 2024, 11:31pm
9
Delta_G. Bit 2 is the 3rd bit.
kavala:
bit(n) of what ?
Assume:
byte m = 0b10101010; //b7, b6, ..., b1, b0
In the above, every bit has --
Positional Factor (PF),
Positional Weight )PW), and
Positional Value (PV).
For example:
For bit-3 (b3): //n = 3
Positional Factor (PF) = 1
Positional Weight (PW) = 2^3 = 8
Positional Value (PV) = PF x PW = 1 x 8 = 8
bit(n) computes/returns the Positional Weight of n-th bit; where, n = 0 refers to b0 , *n = 1 refers to b1 * etc.
Test Sketch:
void setup()
{
Serial.begin(9600);
Serial.print(bit(3), DEC); //shows: 8 = PW of b3
}
void loop() {}
kavala
March 27, 2024, 11:51pm
11
I don't need lessons on bits and bytes thanks.
My question is syntactical.
You are all missing the point. bit(n) only takes one argument, so answer me,
"which byte or variable is the bit in question coming from" ?
Strangely enough, this compiles...
x = bit(0), y;
Your question was succinctly answered by @Delta_G back in post 7 .
westfw
March 28, 2024, 12:43am
15
It's shorthand for "the numeric value when bit N set (and all other bits in the value are 0)"
No additional arguments required.
It's more like the 0x prefix than an actual function.
kavala
March 28, 2024, 1:52am
16
I'm with ya now Delta_G. You were talking about powers of 2, not bit position.
kavala
March 28, 2024, 1:55am
17
bit N of what ??
There is no byte specified in the call ???
kavala
March 28, 2024, 2:27am
19
Ah yes, I think I get it now.
I was assuming bit(n) was returning the value of the bit at position 'n'.
i.e Is bit n a 0 or a 1. Therefore you would need to supply a second argument.
However, I now see it is simply a lookup returning the actual numeric value of 2^n.
Thanks guys for clearing that up.
Kind of a bad attitude given how things turned out.
Comma Operator .
DaveX
March 28, 2024, 3:08am
21
bit( 0): 0b00000000000000000000000000000000 00000000000000000000000000000001
bit( 1): 0b00000000000000000000000000000000 00000000000000000000000000000010
bit( 2): 0b00000000000000000000000000000000 00000000000000000000000000000100
bit( 3): 0b00000000000000000000000000000000 00000000000000000000000000001000
bit( 4): 0b00000000000000000000000000000000 00000000000000000000000000010000
bit( 5): 0b00000000000000000000000000000000 00000000000000000000000000100000
bit( 6): 0b00000000000000000000000000000000 00000000000000000000000001000000
bit( 7): 0b00000000000000000000000000000000 00000000000000000000000010000000
bit( 8): 0b00000000000000000000000000000000 00000000000000000000000100000000
bit( 9): 0b00000000000000000000000000000000 00000000000000000000001000000000
bit(10): 0b00000000000000000000000000000000 00000000000000000000010000000000
bit(11): 0b00000000000000000000000000000000 00000000000000000000100000000000
bit(12): 0b00000000000000000000000000000000 00000000000000000001000000000000
bit(13): 0b00000000000000000000000000000000 00000000000000000010000000000000
bit(14): 0b00000000000000000000000000000000 00000000000000000100000000000000
bit(15): 0b00000000000000000000000000000000 00000000000000001000000000000000
bit(16): 0b00000000000000000000000000000000 00000000000000010000000000000000
bit(17): 0b00000000000000000000000000000000 00000000000000100000000000000000
bit(18): 0b00000000000000000000000000000000 00000000000001000000000000000000
bit(19): 0b00000000000000000000000000000000 00000000000010000000000000000000
bit(20): 0b00000000000000000000000000000000 00000000000100000000000000000000
bit(21): 0b00000000000000000000000000000000 00000000001000000000000000000000
bit(22): 0b00000000000000000000000000000000 00000000010000000000000000000000
bit(23): 0b00000000000000000000000000000000 00000000100000000000000000000000
bit(24): 0b00000000000000000000000000000000 00000001000000000000000000000000
bit(25): 0b00000000000000000000000000000000 00000010000000000000000000000000
bit(26): 0b00000000000000000000000000000000 00000100000000000000000000000000
bit(27): 0b00000000000000000000000000000000 00001000000000000000000000000000
bit(28): 0b00000000000000000000000000000000 00010000000000000000000000000000
bit(29): 0b00000000000000000000000000000000 00100000000000000000000000000000
bit(30): 0b00000000000000000000000000000000 01000000000000000000000000000000
bit(31): 0b00000000000000000000000000000000 10000000000000000000000000000000
bit(32): 0b00000000000000000000000000000000 00000000000000000000000000000000
bit(33): 0b00000000000000000000000000000000 00000000000000000000000000000000
bit(34): 0b00000000000000000000000000000000 00000000000000000000000000000000
bit(35): 0b00000000000000000000000000000000 00000000000000000000000000000000
bit(36): 0b00000000000000000000000000000000 00000000000000000000000000000000
bit(37): 0b00000000000000000000000000000000 00000000000000000000000000000000
bit(38): 0b00000000000000000000000000000000 00000000000000000000000000000000
bit(39): 0b00000000000000000000000000000000 00000000000000000000000000000000
bit(40): 0b00000000000000000000000000000000 00000000000000000000000000000000
bit(41): 0b00000000000000000000000000000000 00000000000000000000000000000000
bit(42): 0b00000000000000000000000000000000 00000000000000000000000000000000
bit(43): 0b00000000000000000000000000000000 00000000000000000000000000000000
bit(44): 0b00000000000000000000000000000000 00000000000000000000000000000000
bit(45): 0b00000000000000000000000000000000 00000000000000000000000000000000
bit(46): 0b00000000000000000000000000000000 00000000000000000000000000000000
bit(47): 0b00000000000000000000000000000000 00000000000000000000000000000000
bit(48): 0b00000000000000000000000000000000 00000000000000000000000000000000
bit(49): 0b00000000000000000000000000000000 00000000000000000000000000000000
bit(50): 0b00000000000000000000000000000000 00000000000000000000000000000000
bit(51): 0b00000000000000000000000000000000 00000000000000000000000000000000
bit(52): 0b00000000000000000000000000000000 00000000000000000000000000000000
bit(53): 0b00000000000000000000000000000000 00000000000000000000000000000000
bit(54): 0b00000000000000000000000000000000 00000000000000000000000000000000
bit(55): 0b00000000000000000000000000000000 00000000000000000000000000000000
bit(56): 0b00000000000000000000000000000000 00000000000000000000000000000000
bit(57): 0b00000000000000000000000000000000 00000000000000000000000000000000
bit(58): 0b00000000000000000000000000000000 00000000000000000000000000000000
bit(59): 0b00000000000000000000000000000000 00000000000000000000000000000000
bit(60): 0b00000000000000000000000000000000 00000000000000000000000000000000
bit(61): 0b00000000000000000000000000000000 00000000000000000000000000000000
bit(62): 0b00000000000000000000000000000000 00000000000000000000000000000000
bit(63): 0b00000000000000000000000000000000 00000000000000000000000000000000
1ULL<< 0: 0b00000000000000000000000000000000 00000000000000000000000000000001
1ULL<< 1: 0b00000000000000000000000000000000 00000000000000000000000000000010
1ULL<< 2: 0b00000000000000000000000000000000 00000000000000000000000000000100
1ULL<< 3: 0b00000000000000000000000000000000 00000000000000000000000000001000
1ULL<< 4: 0b00000000000000000000000000000000 00000000000000000000000000010000
1ULL<< 5: 0b00000000000000000000000000000000 00000000000000000000000000100000
1ULL<< 6: 0b00000000000000000000000000000000 00000000000000000000000001000000
1ULL<< 7: 0b00000000000000000000000000000000 00000000000000000000000010000000
1ULL<< 8: 0b00000000000000000000000000000000 00000000000000000000000100000000
1ULL<< 9: 0b00000000000000000000000000000000 00000000000000000000001000000000
1ULL<<10: 0b00000000000000000000000000000000 00000000000000000000010000000000
1ULL<<11: 0b00000000000000000000000000000000 00000000000000000000100000000000
1ULL<<12: 0b00000000000000000000000000000000 00000000000000000001000000000000
1ULL<<13: 0b00000000000000000000000000000000 00000000000000000010000000000000
1ULL<<14: 0b00000000000000000000000000000000 00000000000000000100000000000000
1ULL<<15: 0b00000000000000000000000000000000 00000000000000001000000000000000
1ULL<<16: 0b00000000000000000000000000000000 00000000000000010000000000000000
1ULL<<17: 0b00000000000000000000000000000000 00000000000000100000000000000000
1ULL<<18: 0b00000000000000000000000000000000 00000000000001000000000000000000
1ULL<<19: 0b00000000000000000000000000000000 00000000000010000000000000000000
1ULL<<20: 0b00000000000000000000000000000000 00000000000100000000000000000000
1ULL<<21: 0b00000000000000000000000000000000 00000000001000000000000000000000
1ULL<<22: 0b00000000000000000000000000000000 00000000010000000000000000000000
1ULL<<23: 0b00000000000000000000000000000000 00000000100000000000000000000000
1ULL<<24: 0b00000000000000000000000000000000 00000001000000000000000000000000
1ULL<<25: 0b00000000000000000000000000000000 00000010000000000000000000000000
1ULL<<26: 0b00000000000000000000000000000000 00000100000000000000000000000000
1ULL<<27: 0b00000000000000000000000000000000 00001000000000000000000000000000
1ULL<<28: 0b00000000000000000000000000000000 00010000000000000000000000000000
1ULL<<29: 0b00000000000000000000000000000000 00100000000000000000000000000000
1ULL<<30: 0b00000000000000000000000000000000 01000000000000000000000000000000
1ULL<<31: 0b00000000000000000000000000000000 10000000000000000000000000000000
1ULL<<32: 0b00000000000000000000000000000001 00000000000000000000000000000000
1ULL<<33: 0b00000000000000000000000000000010 00000000000000000000000000000000
1ULL<<34: 0b00000000000000000000000000000100 00000000000000000000000000000000
1ULL<<35: 0b00000000000000000000000000001000 00000000000000000000000000000000
1ULL<<36: 0b00000000000000000000000000010000 00000000000000000000000000000000
1ULL<<37: 0b00000000000000000000000000100000 00000000000000000000000000000000
1ULL<<38: 0b00000000000000000000000001000000 00000000000000000000000000000000
1ULL<<39: 0b00000000000000000000000010000000 00000000000000000000000000000000
1ULL<<40: 0b00000000000000000000000100000000 00000000000000000000000000000000
1ULL<<41: 0b00000000000000000000001000000000 00000000000000000000000000000000
1ULL<<42: 0b00000000000000000000010000000000 00000000000000000000000000000000
1ULL<<43: 0b00000000000000000000100000000000 00000000000000000000000000000000
1ULL<<44: 0b00000000000000000001000000000000 00000000000000000000000000000000
1ULL<<45: 0b00000000000000000010000000000000 00000000000000000000000000000000
1ULL<<46: 0b00000000000000000100000000000000 00000000000000000000000000000000
1ULL<<47: 0b00000000000000001000000000000000 00000000000000000000000000000000
1ULL<<48: 0b00000000000000010000000000000000 00000000000000000000000000000000
1ULL<<49: 0b00000000000000100000000000000000 00000000000000000000000000000000
1ULL<<50: 0b00000000000001000000000000000000 00000000000000000000000000000000
1ULL<<51: 0b00000000000010000000000000000000 00000000000000000000000000000000
1ULL<<52: 0b00000000000100000000000000000000 00000000000000000000000000000000
1ULL<<53: 0b00000000001000000000000000000000 00000000000000000000000000000000
1ULL<<54: 0b00000000010000000000000000000000 00000000000000000000000000000000
1ULL<<55: 0b00000000100000000000000000000000 00000000000000000000000000000000
1ULL<<56: 0b00000001000000000000000000000000 00000000000000000000000000000000
1ULL<<57: 0b00000010000000000000000000000000 00000000000000000000000000000000
1ULL<<58: 0b00000100000000000000000000000000 00000000000000000000000000000000
1ULL<<59: 0b00001000000000000000000000000000 00000000000000000000000000000000
1ULL<<60: 0b00010000000000000000000000000000 00000000000000000000000000000000
1ULL<<61: 0b00100000000000000000000000000000 00000000000000000000000000000000
1ULL<<62: 0b01000000000000000000000000000000 00000000000000000000000000000000
1ULL<<63: 0b10000000000000000000000000000000 00000000000000000000000000000000
from
#include <LibPrintf.h>
void setup() {
Serial.begin(115200);
for ( int i = 0; i < 64 ; ++i) {
uint64_t j = 1ULL;
j = bit(i); // only works to 31
printf("bit(%2i): 0b%032lb %032lb\n", i, uint32_t(j >> 32), uint32_t(j & 0xFFFFFFFF) );
}
for ( int i = 0; i < 64 ; ++i) {
uint64_t j = 1ULL;
j = 1ULL<<i; // works to 63
printf("1ULL<<%2i: 0b%032lb %032lb\n", i, uint32_t(j >> 32), uint32_t(j & 0xFFFFFFFF) );
}
}
void loop() {
}
Of what ? LOL.
This should have been all done with post #2 . Or 3. Or 4. Or 7. Hours ago.
It's easy I guess to get stuck on an idea of what must be, and simply ignore responses, documentation and code examples.
a7
Does not compile for me in Arduino UNO?
void setup()
{
Serial.begin(9600);
byte y = 0b10101010;
byte x = bit(0), y;
Serial.println(x, DEC);
}
void loop() {}
Yes! I read the error message -- redeclaration of 'byte y'.
I have declared 'y' only once in post #24 ; so, I ignored the above error message; instead, I am wandering to see in what way the following statement is wrong.
byte x = bit(0), y;