I have the following snippet that seems to be taking a total of 6 CPU cycles per invocation.

Can I speed this up somehow? x is a byte with the 3 lowest bits set.

#define SET_COLOR(x) PORTB = (PORTB & ~(7)) | x
  • 1
    @TisteAndii: Interesting suggestion, but it ends up using an identical amount of instructions. I assume the compiler is smart enough. Commented Sep 9, 2016 at 3:04
  • @tisteandii compilers are smart enough to do this for you.
    – Gerben
    Commented Sep 9, 2016 at 13:35
  • @Gerben So I gathered. Dont know much about the optimizations they perform. Commented Sep 9, 2016 at 13:40

1 Answer 1


4 cycles:

CBR Rd,7
ORI Rd,x

Translation into proper GCC inline assembly syntax is left as an exercise for the reader.

  • Thanks. In the end (with some proper analysis) it showed the original code was resulting in exactly 4 instructions, as well. Commented Sep 10, 2016 at 22:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.