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

Benjohn

Is there a quick bit operation to implement msb_equal: a function to check if two numbers have the same most significant bit?

For example, 0b000100 and 0b000111 both have 4 as their most significant bit value, so they are most msb_equal. In contrast 0b001111 has 8 as the MSB value, and 0b010000 has 16 as it's MSB value, so the pair are not msb_equal.

Similarly, are there fast ways to compute <, and <=?

Examples:

msb_equal(0, 0) => true
msb_equal(2, 3) => true

msb_equal(0, 1) => false
msb_equal(1, 2) => false
msb_equal(3, 4) => false

msb_equal(128, 255) => true

A comment asks why 0 and 1 are not msb_equal. My view on this is that if I write out two numbers in binary, they are msb_equal when the most significant 1 bit in each is the same bit.

Writing out 2 & 3:

2 == b0010
3 == b0011

In this case, the top most 1 is the same in each number

Writing out 1 & 0:

1 == b0001
0 == b0000

Here, the top most 1s are not the same.

It could be said that as 0 has no top most set bit, msb_equal(0,0) is ambiguous. I'm defining it as true: I feel this is helpful and consistent.

Benjohn

Yes, there are fast bit based operations to compute MSB equality and inequalities.

Note on syntax

I'll provide implementations using c language syntax for bitwise and logical operators:

  • | – bitwise OR. || – logical OR.
  • & – bitwise AND. && – logical AND.
  • ^ – bitwise XOR.

==

msb_equal(l, r) -> bool
{
  return (l^r) <= (l&r)
}

<

This is taken from the Wikipedia page on the Z Order Curve (which is awesome):

msb_less_than(l, r) -> bool
{
  (l < r) && (l < l^r)
}

<=

msb_less_than_equal(l, r) -> bool
{
  (l < r) || (l^r <= l&r)
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

set most significant bit in C

From Dev

How to clear most significant bit in byte?

From Dev

Find most significant set bit in a long

From Dev

How to comparing two x significant doubles correctly

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

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

From Dev

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

From Dev

How do I flip the most significant bit in MIPS?

From Dev

Compare the number of significant digits in two numbers

From Dev

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

From Dev

Comparing two ranges of numbers performantly

From Dev

Comparing two ranges of numbers performantly

From Dev

Comparing Numbers Two number in bash

From Dev

Time complexity for comparing two numbers

From Dev

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

From Dev

split multiple non-delimited numbers to two significant digits

From Dev

split multiple non-delimited numbers to two significant digits

From Dev

How to format Excel numbers so that >= 0.1 has 1 significant digit, but < 0.1 has two significant digits?

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

Most Significant Byte Calculation

From Dev

Hex of most significant nibble

From Dev

Xor starting with the significant bit

From Dev

Clearing least significant bit

From Dev

Least significant bit mips

From Dev

Merge two large textfiles while comparing numbers

Related Related

  1. 1

    set most significant bit in C

  2. 2

    How to clear most significant bit in byte?

  3. 3

    Find most significant set bit in a long

  4. 4

    How to comparing two x significant doubles correctly

  5. 5

    Get the most significant bit from an 8-bit value

  6. 6

    Get the most significant bit from an 8-bit value

  7. 7

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

  8. 8

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

  9. 9

    How do I flip the most significant bit in MIPS?

  10. 10

    Compare the number of significant digits in two numbers

  11. 11

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

  12. 12

    Comparing two ranges of numbers performantly

  13. 13

    Comparing two ranges of numbers performantly

  14. 14

    Comparing Numbers Two number in bash

  15. 15

    Time complexity for comparing two numbers

  16. 16

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

  17. 17

    split multiple non-delimited numbers to two significant digits

  18. 18

    split multiple non-delimited numbers to two significant digits

  19. 19

    How to format Excel numbers so that >= 0.1 has 1 significant digit, but < 0.1 has two significant digits?

  20. 20

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

  21. 21

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

  22. 22

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

  23. 23

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

  24. 24

    Most Significant Byte Calculation

  25. 25

    Hex of most significant nibble

  26. 26

    Xor starting with the significant bit

  27. 27

    Clearing least significant bit

  28. 28

    Least significant bit mips

  29. 29

    Merge two large textfiles while comparing numbers

HotTag

Archive