How to write a constant time function to copy the most significant bit to all bits

Alex Gaynor

I'd like to write a function, in C, which takes the MSB of uint8_t, and if it's set, returns 0xFF and if not 0x00. In short, which returns an integer where all the bits are set to the same value as the MSB.

But I'd like to do it in a completely constant time way, no branches, no array offsets, just mathematical operations which are guaranteed to always touch the same number of bits. And ideally, without any undefined behavior. How can this be done?

Ilmari Karonen

How about:

#define uint8_msb_to_all_bits(x) (0xFF * ((x) >> 7))

or even better:

#define uint8_msb_to_all_bits(x) (-((x) >> 7))

The way these both work is that, if x is an 8-bit unsigned integer, then x >> 7 is 1 if the MSB of x is set, and 0 otherwise. All that remains is then mapping 1 to 0xFF, which can be done either by multiplication, or, in this particular case, simply by negating the number.

(Yes, negating an unsigned number is well defined in C.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to clear most significant bit in byte?

From Dev

How to interpret using Most Significant Bit of single byte - Pcapng time format "if_tsresol"

From Dev

How to write least significant bit into the buffer?

From Dev

How to get most significant n bits from int in java

From Dev

how is the most significant bit radix sort more efficient than the least significant bit radix sort?

From Dev

How do I flip the most significant bit in MIPS?

From Dev

set most significant bit in C

From Dev

Storing data in the most significant bits of a pointer

From Dev

Find most significant set bit in a long

From Dev

Comparing the Most Significant Bit of two numbers: ==, <, <=

From Dev

How can you get the j first most significant bits of an integer in C++?

From Dev

How to change the most significant bit to a 1 after shifting an int to the right using '>>'?

From Dev

Get the most significant bit from an 8-bit value

From Dev

Get the most significant bit from an 8-bit value

From Dev

Significant time spent in function call

From Dev

The MSB (most significant bit ) in pyserial python sent to arduino uno in damged

From Dev

How to Delete Least Significant Bit in Java?

From Dev

Writing a bit reader in JAVA (32-bit little-endian most-to-least-significant bit packing)

From Dev

How do I read and write a stream 3 bits at a time?

From Dev

Fast way of finding most and least significant bit set in a 64-bit integer

From Dev

Subdivide a Modulo function - 16 bit but can only do 8 bits at a time

From Java

Why is the segfault address NULL when accessing memory that has any of the 16 most significant bits set?

From Dev

How to write nodejs service, running all time

From Dev

How can I make my function run in constant time?

From Dev

Create a mask that marks the most significant set bit, using only bitwise operators

From Dev

Most Significant Byte Calculation

From Dev

Hex of most significant nibble

From Dev

How can I get the value of the least significant bit in a number?

From Dev

How to Replace Least Significant Bit In Buffered Image in Scala

Related Related

  1. 1

    How to clear most significant bit in byte?

  2. 2

    How to interpret using Most Significant Bit of single byte - Pcapng time format "if_tsresol"

  3. 3

    How to write least significant bit into the buffer?

  4. 4

    How to get most significant n bits from int in java

  5. 5

    how is the most significant bit radix sort more efficient than the least significant bit radix sort?

  6. 6

    How do I flip the most significant bit in MIPS?

  7. 7

    set most significant bit in C

  8. 8

    Storing data in the most significant bits of a pointer

  9. 9

    Find most significant set bit in a long

  10. 10

    Comparing the Most Significant Bit of two numbers: ==, <, <=

  11. 11

    How can you get the j first most significant bits of an integer in C++?

  12. 12

    How to change the most significant bit to a 1 after shifting an int to the right using '>>'?

  13. 13

    Get the most significant bit from an 8-bit value

  14. 14

    Get the most significant bit from an 8-bit value

  15. 15

    Significant time spent in function call

  16. 16

    The MSB (most significant bit ) in pyserial python sent to arduino uno in damged

  17. 17

    How to Delete Least Significant Bit in Java?

  18. 18

    Writing a bit reader in JAVA (32-bit little-endian most-to-least-significant bit packing)

  19. 19

    How do I read and write a stream 3 bits at a time?

  20. 20

    Fast way of finding most and least significant bit set in a 64-bit integer

  21. 21

    Subdivide a Modulo function - 16 bit but can only do 8 bits at a time

  22. 22

    Why is the segfault address NULL when accessing memory that has any of the 16 most significant bits set?

  23. 23

    How to write nodejs service, running all time

  24. 24

    How can I make my function run in constant time?

  25. 25

    Create a mask that marks the most significant set bit, using only bitwise operators

  26. 26

    Most Significant Byte Calculation

  27. 27

    Hex of most significant nibble

  28. 28

    How can I get the value of the least significant bit in a number?

  29. 29

    How to Replace Least Significant Bit In Buffered Image in Scala

HotTag

Archive