C/C++: Get Help on Unsigned Int x &= x-1;

  • Context: Comp Sci 
  • Thread starter Thread starter andytran
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around the operation `x &= x-1;` in C/C++, specifically its function and implications for counting set bits (ones) in an unsigned integer. Participants explore the mechanics of the operation and its potential advantages over traditional looping methods for bit counting.

Discussion Character

  • Exploratory, Technical explanation, Conceptual clarification, Homework-related

Main Points Raised

  • One participant seeks clarification on what `x &= x-1;` does and why it works, indicating a lack of understanding of the underlying mechanics.
  • Another participant prompts for prior knowledge about the bitwise AND operator, suggesting that understanding this is crucial for grasping the operation in question.
  • A different participant encourages consideration of how to loop through bit positions for counting set bits, hinting at alternative methods that may be faster than the `&=` approach.
  • A participant explains the operation by providing a specific example with the number 5, detailing the binary representation and the result of the operation.

Areas of Agreement / Disagreement

The discussion does not appear to reach a consensus, as participants express varying levels of understanding and propose different methods for counting set bits, indicating multiple competing views on the best approach.

Contextual Notes

Some assumptions about the understanding of bitwise operations and the context of the problem may be missing, as participants have different levels of familiarity with the topic. The discussion also hints at the existence of potentially faster methods for counting set bits, but these are not fully explored.

Who May Find This Useful

This discussion may be useful for individuals interested in C/C++ programming, particularly those looking to understand bit manipulation techniques and efficient algorithms for counting bits in integers.

andytran
Messages
41
Reaction score
0
Hi,

After hrs of googling still couldn't find the answer to my question. I'm hoping i get better luck positng it here. Any C/C++ out there please help!

1)
a) Given an unsigned int x, what does x &= x-1; do and why does it work?

I know what x &= x-1 does but to explain why does it work, i have no clue.

b) How can the statement in a) be used in a C++ program to count set bits (ones) in a given unsigned int x? What is the advantage over looping through all bit positions?


thanks in advance.
 
Physics news on Phys.org
For starters, tell us what you already know. What does the & operator do in C?

- Warren
 
Well for 2b), think about how you would loop through the bit positions. What are you going to change for each iteration of the loop? Also think about the different ways that you could do this counting problem, and whether this &= method would have advantages over other ways... BTW, I think there is at least one faster way to do the counting...can you think of it too?
 
chroot said:
For starters, tell us what you already know. What does the & operator do in C?

- Warren

& is the bitwise and operator, x &= x-1; is the same as x = x & (x-1);

if an unsigned int x = 5; then

5 = 101 in binary and (x-1) = 4 = 100

so after evaluating, x = 100 in binary or x = 4 in decimal.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 23 ·
Replies
23
Views
9K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K