What is the fastest way to get the 4 least significant bits in a byte (C++)?

后端 未结 6 2098
孤街浪徒
孤街浪徒 2021-02-09 07:12

I\'m talking about this:

If we have the letter \'A\' which is 77 in decimal and 4D in Hex. I am looking for the fastest way to get D.

I thought about two ways:

6条回答
  •  我在风中等你
    2021-02-09 07:44

    There are many good answers and some of them are technically the right ones.

    In a broader scale, one should understand that C/C++ is not an assembler. Programmer's job is to try to tell to the compiler the intention what you want to achieve. The compiler will pick the best way to do it depending on the architecture and various optimization flags.

    x &= 0x0F; is the most clear way to tell the compiler what you want to achieve. If shifting up and down is faster on some architecture, it is the compiler's job to know it and do the right thing.

提交回复
热议问题