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

beachwood23

This was part of a larger programming assignment that was due for me last night. Couldn't figure out this problem, but I'm curious as to how it could be solved.

The function int greatestBitPos(int x) should return an int mask that marks the position of the most significant bit. If x==0, return 0. No control structures (if, while, ?:) allowed.

Example: greatestBitPos(96) = 0x40

Legal operators: ! ~ & ^ | + << >> =

This website on bit twiddling is something I used as a starting point, especially the second algorithm. However, it uses < comparisons, something this problem doesn't allow.

All ideas are welcome, thanks!

Edit: Please assume 2's complement, 32-bit integers. For all negative numbers, they have their topmost bit set, so the return value should be 0x80000000.

Floris

Updated to work for negative numbers (assuming that this should return 0x80000000 since these numbers have their top bit set )

int gbp(int n) {
 // return log(2) of n
 unsigned int m;
 m = n;
 m = m | m >> 1;
 m = m | m >> 2;
 m = m | m >> 4;
 m = m | m >> 8;
 m = m | m >> 16;
 m = m & ((~m >> 1)^0x80000000);
printf("m is now %d\n", m);
return m;
}

Explanation:

Starting with any bit pattern, when we shift right by 1 and take the OR, adjacent bits will become 1. Example

00010100
00001010
--------
00011110

You repeat this until you have all ones to the right of the leading digit, by successively shifting 2, 4, 8, 16 (if you have 32 bit numbers; for larger int you keep going).

Finally you need to "strip all the other ones" by inverting the number, right shifting by 1, and taking the AND:

00011111 AND 11110000 = 00010000

and there you have it.

For negative numbers, the final manipulation ensures that you don't kill the top bit if it exists. If you wanted something else to be done with negative numbers, let me know what it is.

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

Find most significant set bit in a long

From Dev

Bitwise operators: How do I clear the most significat bit?

From Dev

Calculate exponents of 16 using only bitwise operators

From Dev

Replicating the function of a for loop using only bitwise operators

From Dev

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

From Dev

sign function in C using bit operators only

From Dev

Multiplying using Bitwise Operators

From Dev

Computing the floor of log₂(x) using only bitwise operators in C

From Dev

Rotate right by n only using bitwise operators in C

From Dev

How to clear most significant bit in byte?

From Dev

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

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

Eliminating IF statement using bitwise operators

From Dev

How to solve using bitwise operators

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

Index of second least significant set bit

From Dev

Efficient least significant set bit of "biginteger" class

From Dev

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

From Dev

Do this using bit operators

From Dev

Bitwise operators in java only for integer and long?

From Dev

Bitwise operators in java only for integer and long?

From Dev

How can I express the term ( a < b ? 0 : 1 ) using only bitwise or arithmetic operators?

From Dev

Extract Most Significant Nibble using Swift

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

samba: create mask & force create mode can't set group write bit

Related Related

  1. 1

    set most significant bit in C

  2. 2

    Find most significant set bit in a long

  3. 3

    Bitwise operators: How do I clear the most significat bit?

  4. 4

    Calculate exponents of 16 using only bitwise operators

  5. 5

    Replicating the function of a for loop using only bitwise operators

  6. 6

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

  7. 7

    sign function in C using bit operators only

  8. 8

    Multiplying using Bitwise Operators

  9. 9

    Computing the floor of log₂(x) using only bitwise operators in C

  10. 10

    Rotate right by n only using bitwise operators in C

  11. 11

    How to clear most significant bit in byte?

  12. 12

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

  13. 13

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

  14. 14

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

  15. 15

    Eliminating IF statement using bitwise operators

  16. 16

    How to solve using bitwise operators

  17. 17

    Get the most significant bit from an 8-bit value

  18. 18

    Get the most significant bit from an 8-bit value

  19. 19

    Index of second least significant set bit

  20. 20

    Efficient least significant set bit of "biginteger" class

  21. 21

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

  22. 22

    Do this using bit operators

  23. 23

    Bitwise operators in java only for integer and long?

  24. 24

    Bitwise operators in java only for integer and long?

  25. 25

    How can I express the term ( a < b ? 0 : 1 ) using only bitwise or arithmetic operators?

  26. 26

    Extract Most Significant Nibble using Swift

  27. 27

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

  28. 28

    How do I flip the most significant bit in MIPS?

  29. 29

    samba: create mask & force create mode can't set group write bit

HotTag

Archive