c++ - Calculate how many ones in bits and bits inverting -


संभव डुप्लिकेट:

नमस्कार

बिट्स में कितने लोगों की गणना करना है?

  1100110 - & gt; 4 101 - & gt; 2   

और दूसरा प्रश्न:

बिट्स को कैसे उलट करना है?

  1100110 - & gt; 0011001 101 - & gt; 010   

धन्यवाद

  1. आप कर सकते हैं लूप जबकि संख्या शून्य है, और जब अंतिम बिट सेट हो जाती है तो एक काउंटर बढ़ता है। या यदि आप इंटेल आर्किटेक्चर पर काम कर रहे हैं, तो आप इनलाइन विधानसभा में popcnt निर्देश का उपयोग कर सकते हैं।

      int count_bit_set (अहस्ताक्षरित int x) {int count = 0; जबकि (x! = 0) {count + = (x और 1); X = x> और gt; 1; } वापसी की गणना; }    
  2. आप ~ ऑपरेटर का उपयोग करते हैं।

Comments

Popular posts from this blog

qt - switch/case statement in C++ with a QString type -

python - sqlite3.OperationalError: near "REFERENCES": syntax error - foreign key creating -

Python's equivalent for Ruby's define_method? -