Hi,
sorry if this has been asked before, (couldn't find exactly this question) but isn't
bit(x)
equivalent to
1<<x
?
Or is there a subtle difference?
Thank you.
Hi,
sorry if this has been asked before, (couldn't find exactly this question) but isn't
bit(x)
equivalent to
1<<x
?
Or is there a subtle difference?
Thank you.
From Arduino.h:
#define bit(b) (1UL << (b))
Oh ok, should have checked that
thanks (still observing the diffs between g++ and this one).
Maybe it's just me, but I find the '<<' version more readable.
Agree.
At least it is obvious, no need to track down some macro somewhere.
a7
...but there's always the tendency to write something like " x &= 1 << 17;" and then wondering why it doesn't work.
Yeah well... You're right on this one
(so, here is the "subtle" difference if the sizeof(type) > sizeof(int) and one forgets the L/UL... )
TheMemberFormerlyKnownAsAWOL:
...but there's always the tendency to write something like " x &= 1 << 17;" and then wondering why it doesn't work.
Well I never mind adding parentheses as I can't keep it all sorted. Here however, it seems that it does work, or at least does do what I read you to intend:
# include <stdio.h>
int main()
{
int jj = 0x0ffff;
jj &= 1 << 7;
printf("Hello World %x", jj);
return 0;
}
Yields
Hello World 80
I am sure to be missing something.
a7
alto777:
I am sure to be missing something.a7
The fact that I wrote " 1 << 17;" ?
TheMemberFormerlyKnownAsAWOL:
...but there's always the tendency to write something like " x &= 1 << 17;" and then wondering why it doesn't work.
Works fine on the esp8266... ![]()
On the AVR at least you get a warning that it isn't going to work if you have them enabled.
--- bill
I also have no use for those kind of macros, that do something so simple that what it does seems simpler than using it.
aarg:
I also have no use for those kind of macros, that do something so simple that what it does seems simpler than using it.
I find them helpful.
I've been using these types of C macros on embedded projects for close to 40 years.
I think that for the typical Arduino user that is less experienced in C coding, and less likely to understand integer expression evaluations, particularly some of the gotchyas of 16 bit integers,
this bit() macro is quite helpful.
This type of macro avoids the issue of getting burned by what TheMemberFormerlyKnownAsAWOL brought up as I'm guessing that most Arduino users would not see the issue with the expression:
x &= 1 << 17;
when used on the AVR processor.
--- bill
Yes, but most users would never write an expression like that. ![]()
aarg:
Yes, but most users would never write an expression like that.
touche
but they might accidentally write something like:
if( x & (1 << 17) )
x |= (1 <<17);
x &= ~(1 << 17);
and get unexpectedly burned on the AVR even though they declared x as a uint32_t
Whereas if they used the bit() macro, it wouldn't be an issue.
i.e.
if(x & bit(17))
x |= bit(17);
x &= ~bit(17);
It can be less obvious is if there additional macros involved.
i.e.
#define MAGICBIT (1 << 17)
if(x & MAGICBIT)
fails to work on the AVR.
IMO, having warnings disabled by default in the IDE is doing users a disservice since, without warnings, there are many coding issues that can silently generate intended and unexpected behaviors.
--- bill
Well as usual staring at a reiteration of your "clue" was of no use to me.
I figured it out in the middle of my sandwich. I think it is mostly my inability to recognize a kind of error I just don't usually make... and a perfect case of where seeking answers on a real computer would be misleading.
In any case, as mentioned a compiler warning is generated on the Arduino, so no one has any excuse to wonder why it doesn't work.
I think if arduino.h was explicitly included it might reduce my objection an iota or two. Just one more thing under the hood meant to make thing easier which doesn't always necessarily.
a7
No-one going to play the portability trump card?
Ho-hum.
'Night, all.
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.