1

I've had a series of bugs over the last month due to mistyping of "&" instead of "&&" in C.

My latest one was simple - i.e. I wrote

value = data && 0x0123

It was obvious after I hit a break and saw the value was 0 or 1, but I want a way to catch this earlier, similar to coding

if (1 == END) ... 

to catch misuse of = and ==.

Any ideas?

Alex K
  • 8,269
  • 9
  • 39
  • 57
Larry_C
  • 316
  • 1
  • 9
  • 2
    Just write good code?! It's simple – Rizier123 Jan 13 '15 at 19:45
  • 4
    Note, [yoda conditions should not be needed with modern compiler warnings](http://stackoverflow.com/a/22106732/1708801) – Shafik Yaghmour Jan 13 '15 at 19:45
  • 6
    Since you tagged C++: in C++ you can use `and` and `bitand` instead of `&&` and `&`. [See here](http://coliru.stacked-crooked.com/a/fd8078b1169d6752) – Borgleader Jan 13 '15 at 19:46
  • Use a checklist. After you are finished editing, apply your checklist to each modified file before compiling. – Thomas Matthews Jan 13 '15 at 19:46
  • 2
    Can't say I've ever had this problem. – Lightness Races in Orbit Jan 13 '15 at 19:54
  • @LightnessRacesinOrbit Neither have I. Then again I rarely do bitwise ops. – Borgleader Jan 13 '15 at 19:55
  • Similarly with `==` and `=` but these are just as easy to spot. – Weather Vane Jan 13 '15 at 19:56
  • 2
    @LightnessRacesinOrbit is right. It doesn't make sense to make that mistake unless you didn't know the difference between `&` & `&&` and now you found out, and don't want to search your code for such issues, I would recommend using grep to search for all possible occurrences of both `&` & `&&` and fix each one. – Iharob Al Asimi Jan 13 '15 at 19:56
  • @iharob I think it does make sense, in general, that a typo could end up compiling. I've certainly seen professionals do it. – Drew Dormann Jan 13 '15 at 20:00
  • So are you using a C or C++ compiler the specifics change depending on this. – Shafik Yaghmour Jan 13 '15 at 20:00
  • 1
    @LightnessRacesinOrbit: I found an example of this recently. It had apparently been there for a while but hadn't manifested as a problem. It was not my code. – Fred Larson Jan 13 '15 at 20:00
  • 1
    A friend of mine asked me once, why did I use `&&` when I could use `&`?. He had been programming in his thesis project for about 5 months and he didn't know the difference. – Iharob Al Asimi Jan 13 '15 at 20:09
  • @Rizier123 Nice you offered no solution, just sarcasm - sympton of too much time on SE unfortunately. – Larry_C Dec 07 '17 at 19:42
  • @Iharob Al Asim What it means is that you aren't an embedded developer where you mix logical and bitwise ops all the time. Or maybe you don't write a lot of code anymore, or where you work productivity doesn't matter, so you don't try to come up with solutions. Or maybe all three apply. Sorry dude, but you deserved it. – Larry_C Dec 07 '17 at 19:48
  • @Larry_C Of course I don't come up with solutions, I design them and they are born like a baby, in my brain. If you mix operators, you should think why are you doing something wrong and why is it working? Will it always work? Also, productivity is not writing code quickly, it's writing code with fewer bugs. You appear to be a pro-cowboy programmer programmer. I don't hire cowboy programmers. BTW I understand that you "*mix*" means that you confuse them. It just happens that I am very careful with style and extremely consistent so these things don't happen (*others do, but not these*). – Iharob Al Asimi Dec 08 '17 at 03:11

1 Answers1

8

In C++, and and bitand are alternatives for && and & respectively (You can find all the alternatives on cppreference).

#include <iostream>

int main()
{
    std::cout << std::boolalpha << (42 and 0x0123) << std::endl;
    std::cout << std::boolalpha << (42 bitand 0x0123) << std::endl;
    return 0;
}

Live on Coliru (Thanks to Fred Larson for the std::boolalpha suggestion)

C99 defines these as macros in iso646.h (Credit goes to Shafik Yaghmour, more details in one of his answers)

Community
  • 1
  • 1
Borgleader
  • 15,826
  • 5
  • 46
  • 62
  • 2
    Since the question is also tagged C, you should also explain that [iso646.h](http://stackoverflow.com/a/18186331/1708801) is needed. Note, I am not the downvoter. – Shafik Yaghmour Jan 13 '15 at 19:56
  • I think [adding `std::boolalpha`](http://coliru.stacked-crooked.com/a/bbf3cba799811dc1) makes your example code even more obvious. – Fred Larson Jan 13 '15 at 19:57
  • It might be worth noting that `or` and `bitor` can be substituted for `||` and `|` respectively as well. – Fred Larson Jan 13 '15 at 20:08
  • @FredLarson Added a link to the cppreference page, it has a table with all of them. – Borgleader Jan 13 '15 at 20:10
  • 1
    `` was actually added to C by an amendment in 1995 (ANSI/ISO/IEC 9899-1990-AM 1-1995), so pre-C99 compilers are likely to support it. – Keith Thompson Jan 13 '15 at 23:45