Maanu
Maanu

Reputation: 5203

Optimized way to find new bits on

We need to find new bits turned ON in interlock status received from the device compared to the last status read. This is for firing error codes for bits that are newly set. I am using the following statement.

bits_on =~last_status & new_status;

Is there any better ways to do this?

Upvotes: 2

Views: 46

Answers (1)

samgak
samgak

Reputation: 24427

It's only 2 operations and an assignment, so the only way to improve it would be to do it in 1 operation and an assignment. It doesn't correspond to any of the simple C bit manipulation operators, so doing it in 1 operation is not possible.

However, depending on your architecture your compiler might actually already be compiling it to a single instruction.

ANDN (Logical AND NOT) is part of the BMI1 instruction set, and is equivalent to ~x & y.

Upvotes: 4

Related Questions