TRISAbits
TRISAbits

Reputation: 465

Why use the '+' operator when '|' is perfectly good?

This is more of a philosophical question, but I've seen this a bunch of times in codebases here and there and do not really understand how this programming method came to be.

Suppose you have to set bits 2 and 3 to some value x without changing the other values in the uint. Doing so is pretty trivial and a common task, and I would be inclined to do it this way:

uint8_t someval = 0xFF; //some random previous value
uint8_t x = 0x2; //some random value to assign.
someval = (somval & ~0xC) | (x << 2); //Set the value to 0x2 for bits 2-3

I've seen code that instead or using '|' uses '+':

uint8_t someval = 0xFF; //some random previous value
uint8_t x = 0x2; //some random value to assign.
someval = (somval & ~0xC) + (x << 2); //Set the value to 0x2 for bits 2-3

Are they equivalent?
Yes.

Is one better than the other?
Only if your hardware doesn't have a bitwise OR instruction, but I have never ever ever seen a processor that didn't have a bitwise OR (even small PIC10 processors have an OR instruction).

So why would some programmers be inclined to use '+' instead of '|'? Am I missing some really obvious, uber powerful optimization here?

Upvotes: 1

Views: 205

Answers (4)

Mike Seymour
Mike Seymour

Reputation: 254431

Are they equivalent?

Yes, as long as the bitfield being written to is clear beforehand. Otherwise, they'll go wrong in slightly different ways.

Is one better than the other?

No, although some would say that bitwise operations express the intent more clearly.

So why would some programmers be inclined to use '+' instead of '|'?

Because they're equivalent, and neither is particularly better than the other.

Am I missing some really obvious, uber powerful optimization here?

No.

Upvotes: 4

Adam Rosenfield
Adam Rosenfield

Reputation: 400174

It's just a question of style. Any modern CPU will complete both operations in the same number of cycles (typically 1). Personally I prefer | in these cases since it more explicitly states to the code reader that you're doing bit twiddling instead of arithmetic.

If you have a bug in your code, then using + could lead to strange behavior, whereas using | would tend to mask the bug. For example, if you accidentally include the same bit twice, ORing it again is a no-op, but adding it will clear the bit and carry up into the next bit (and possibly farther, if more bits are set). So that would usually lead to fail-fast behavior instead of failure-masking behavior, which is generally preferable.

Upvotes: 0

Luchian Grigore
Luchian Grigore

Reputation: 258558

So why would some programmers be inclined to use '+' instead of '|'?

+ could bring out logical bugs faster. a | a would appear to work, whereas a simple a + a definitely wouldn't (of course, depends on the logic, but the + version is more error-prone).

Of course you should stick to the standard way of doing things (use bitwise operations when you want a bitwise operation, and arithmetic operations when you want to do math).

Upvotes: 0

Kerrek SB
Kerrek SB

Reputation: 476950

  • If you want to perform bitwise operations, use bitwise operators.

  • If you want to perform arithmetic operations, use arithmetic operators.

It's true that for some values some arithmetic operations can be implemented as simple bitwise operations, but that's essentially an implementation detail to which you should never expose your readers. First and foremost the logic of the code should be clear and if possible self-explanatory. The compiler will choose appropriate low-level operations for you to implement your desire.

That's being philanthropic.

Upvotes: 5

Related Questions