Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Get number of 1s in a set of binary numbers

Get number of 1s in a set of binary numbers

Scheduled Pinned Locked Moved C / C++ / MFC
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hearties
    wrote on last edited by
    #1

    is there a functions of method where I can obtain the number of 1s in a set of binary numbers? eg. I have data = 59; //00111011; and I get nCount = 5; // since there are 5 ones in the binary equivalent of 59 Thanks in advance.

    M M 2 Replies Last reply
    0
    • H hearties

      is there a functions of method where I can obtain the number of 1s in a set of binary numbers? eg. I have data = 59; //00111011; and I get nCount = 5; // since there are 5 ones in the binary equivalent of 59 Thanks in advance.

      M Offline
      M Offline
      Mark Jones
      wrote on last edited by
      #2

      This should do what you want. int GetHighBitCount(int i) { int iResult=0; while (i) { // Mask for least significant bit of 'i' if (i & 0x00000001) iResult++; // Right shift 'i' by one bit i >>= 1; } return iResult; } Mark Jones Software Engineer Hampshire UK

      1 Reply Last reply
      0
      • H hearties

        is there a functions of method where I can obtain the number of 1s in a set of binary numbers? eg. I have data = 59; //00111011; and I get nCount = 5; // since there are 5 ones in the binary equivalent of 59 Thanks in advance.

        M Offline
        M Offline
        markkuk
        wrote on last edited by
        #3

        #include <iostream>
        #include <bitset>

        int main(int argc, char* argv[])
        {
        unsigned long data = 59;
        int nCount = std::bitset<32>(data).count();

        std::cout << nCount << endl;
        return 0;
        

        }

        This asssumes 32 bit longs, I don't remember the correct way to portably get the number of bits in a type.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups