Counting Bits To Left/Right in 32-bit Integer Using Bit Operations

  • Thread starter Thread starter noblerare
  • Start date Start date
  • Tags Tags
    Bit Counting
Click For Summary

Discussion Overview

The discussion revolves around the problem of counting the number of bits to the left and right of a given '1' in a 32-bit integer using only bit operations, specifically without employing loops or conditional statements. Participants explore potential methods and solutions within the constraints of bit manipulation.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant inquires about counting bits to the left and right of a '1' in a 32-bit integer, seeking a solution that avoids loops and conditionals.
  • Another participant shares a link to a resource that discusses various bit manipulation techniques, including some that may involve loops.
  • A subsequent reply clarifies that the provided solution includes a for loop, which does not meet the original requirement of avoiding loops.
  • Further responses suggest looking for alternative techniques on the linked page that do not utilize loops.

Areas of Agreement / Disagreement

Participants generally agree on the requirement to find a solution that strictly uses bit operations without loops or conditionals. However, there is disagreement regarding the applicability of the solutions presented in the linked resource, as some involve loops.

Contextual Notes

The discussion is limited by the participants' focus on specific bit operations and the requirement to avoid loops and conditionals, which may restrict the range of viable solutions. There is also a reliance on external resources that may not fully align with the stated constraints.

noblerare
Messages
49
Reaction score
0

Homework Statement



I am wondering is there a way to count the number of bits to the left or right of a given 1 in a 32-bit integer?

For example, if I give the function the number 32 = 0b100000, there are 5 bits to the right of the 1 and hence, 26 bits to the left of the 1.

The catch is, is there a way to do this by simply using bit operations? i.e. no loops or conditionals? Bit operations include: & ^ | << >> ! and ~

(Note: I am coding this in C)

The Attempt at a Solution



Obviously, if I subtract 1 from the original number, I get all 1s to the right of the original number but I'm still stumped as to how I can count the actual number of bits without using loops or conditional statements.
 
Physics news on Phys.org
Thanks, but the link you posted has the solution using a for loop.

Is there a way to do this without using loops or conditional statements? Simply using bit operations such as << >> ! ~ & | ^
 
Not the kernighan solution - but the ones below it (that was the only entry that had an index position)
 
Read on in the page that mgb_phys linked to. The very first routine uses a for loop, but there are other techniques on the page that don't use loops.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
7
Views
14K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
29
Views
5K
Replies
7
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K