a fanny question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • xinxin

    #1

    a fanny question

    hi eyeryone
    001101000000000 000100000000000 000 is a number in binary system.how can
    i get the number of '1's ?
    thanks in advance
  • Joachim Schmitz

    #2
    Re: a fanny question

    xinxin wrote:
    hi eyeryone
    001101000000000 000100000000000 000 is a number in binary system.how can
    i get the number of '1's ?
    Easy: just count them. I count 4.

    Bye, Jojo


    Comment

    • zerg

      #3
      Re: a fanny question

      On 22 abr, 11:10, "Joachim Schmitz" <nospam.j...@sc hmitz-digital.de>
      wrote:
      xinxin wrote:
      hi eyeryone
      001101000000000 000100000000000 000 is a number in binary system.how can
      i get the number of '1's ?
      >
      Easy: just count them. I count 4.
      >
      Bye, Jojo
      Are you using a string?
      Maybe "for" can help you.

      Comment

      • xinxin

        #4
        Re: a fanny question

        On 4ÔÂ22ÈÕ, ÏÂÎç10ʱ03·Ö, xinxin <jnzhouxin...@g mail..comwrote:
        hi eyeryone
        001101000000000 000100000000000 000 is a number in binary system.how can
        i get the number of '1's ?
        thanks in advance
        oh ,i must add something .there is little '1's .and it's not a string
        but
        a unsinged long type. that's to say i what to know how many '1's
        a number in decimal system has when it showed in binary system.anyone
        get any idea.

        Comment

        • bryan-webelos

          #5
          Re: a fanny question

          On Apr 22, 10:03 am, xinxin <jnzhouxin...@g mail.comwrote:
          hi eyeryone
          001101000000000 000100000000000 000 is a number in binary system.how can
          i get the number of '1's ?
          thanks in advance

          Comment

          • Bartc

            #6
            Re: a fanny question

            "xinxin" <jnzhouxinxin@g mail.comwrote in message
            news:759260a4-7c8c-477a-bf1e-4bd02ef22b36@t5 4g2000hsg.googl egroups.com...
            hi eyeryone
            001101000000000 000100000000000 000 is a number in binary system.how can
            i get the number of '1's ?
            thanks in advance
            I used this code. But I'm not sure if it's guaranteed to complete.

            #include <stdio.h>
            #include <stdlib.h>
            #include <limits.h>

            #define int_bits sizeof(unsigned int)*CHAR_BIT

            /* return number of set bits in n */
            int countbits(unsig ned int n) {
            int count,bitno;
            unsigned m;

            count=0;

            while (n) {
            bitno = rand() & (int_bits-1); /* assume 2^k bits */
            m=1;
            if (bitno) m<<=bitno; /* m<<0 legal? don't know */

            if (n & m) {
            ++count;
            n &= (~m);
            };
            };

            return count;
            }

            char* strbits(unsigne d int n) {
            static char s[int_bits+2];
            int i,j;

            j=int_bits;
            s[int_bits]=0;
            for (i=0; i<int_bits; ++i) {
            s[--j] = ((n & 1) ?'1':'0');
            n>>=1;
            };

            return s;
            }

            int main(void)
            {
            int count,bitno;
            unsigned int number=0x555555 55;

            printf("Number = %u\n",number);
            printf(" = %sB\n",strbits( number));
            printf("1 Bits = %d\n",countbits (number));

            }

            --
            Bartc


            Comment

            • santosh

              #7
              Re: a fanny question

              xinxin wrote:

              [misleading subject line]
              hi eyeryone
              001101000000000 000100000000000 000 is a number in binary system.how can
              i get the number of '1's ?
              thanks in advance
              You can use a combnation of shifts and bit masks to calculate the number
              of set bits. Code for this has often been posted to this group. A
              Google search might help.

              Here is a quick and dirty program I cobbled up. Haven't tested it much.

              #include <stdio.h>
              #include <stdlib.h>
              #include <errno.h>
              #include <limits.h>

              unsigned n_setbits(unsig ned long);

              int main(int argc, char **argv) {
              unsigned long n;

              if (argc != 2) {
              puts("Usage: program number.");
              exit(EXIT_FAILU RE);
              }
              else {
              errno = 0;
              n = strtoul(argv[1], NULL, 0);
              if (errno == ERANGE) {
              puts("Conversio n error.");
              exit(EXIT_FAILU RE);
              }
              printf("Set bits = %u\n", n_setbits(n));
              }
              return 0;
              }

              unsigned n_setbits(unsig ned long n) {
              unsigned setbits, ctr;
              unsigned long mask;

              for (ctr = setbits = 0, mask = 1;
              ctr <= sizeof(unsigned long) * CHAR_BIT;
              ctr++, mask <<= 1) {
              if (n & mask) {
              setbits++;
              }
              }
              return setbits;
              }

              Comment

              • Sjouke Burry

                #8
                Re: a fanny question

                xinxin wrote:
                hi eyeryone
                001101000000000 000100000000000 000 is a number in binary system.how can
                i get the number of '1's ?
                thanks in advance
                Count them.

                Comment

                • Peter Nilsson

                  #9
                  Re: a fanny question

                  santosh wrote:
                  unsigned n_setbits(unsig ned long n) {
                  unsigned setbits, ctr;
                  unsigned long mask;
                  >
                  for (ctr = setbits = 0, mask = 1;
                  ctr <= sizeof(unsigned long) * CHAR_BIT;
                  WHY?!!!

                  Work with _values_, not _representation s_.

                  The expression sizeof(unsigned long) * CHAR_BIT needn't
                  match the number of value bits in unsigned long.

                  The mask you're using will become zero when you run out
                  of bits. You don't need to count how many bits there might
                  be when you're already traversing the bits that are.

                  That said, I generally prefer...

                  for (mask = -1, mask = mask / 2 + 1; mask; mask <<= 1)

                  ....because it's portable to unsigned types with rank lower
                  than int. [Note that it's theoretically possible that USHRT_MAX
                  == INT_MAX.]
                  ctr++, mask <<= 1) {
                  if (n & mask) {
                  setbits++;
                  }
                  }
                  return setbits;
                  }
                  --
                  Peter

                  Comment

                  • xinxin

                    #10
                    Re: a fanny question

                    On 4ÔÂ23ÈÕ, ÉÏÎç2ʱ03·Ö, santosh <santosh....@gm ail.comwrote:
                    xinxin wrote:
                    >
                    [misleading subject line]
                    >
                    hi eyeryone
                    001101000000000 000100000000000 000 is a number in binary system.how can
                    i get the number of '1's ?
                    thanks in advance
                    >
                    You can use a combnation of shifts and bit masks to calculate the number
                    of set bits. Code for this has often been posted to this group. A
                    Google search might help.
                    >
                    the idea is great. and it work perfectly. but as i said before ,there
                    is little '1' in the number of binary system. must i get the number of
                    '1's
                    it contains(or includes,i don't know which word to choose) by checking
                    it bit by bit .that's maybe the most fanny as well as most challenging
                    part
                    of this small question

                    Comment

                    • Keith Thompson

                      #11
                      Re: a fanny question

                      xinxin <jnzhouxinxin@g mail.comwrites:
                      On 4月23日, 上午2时03分 , santosh <santosh....@gm ail.comwrote:
                      >xinxin wrote:
                      >>
                      >[misleading subject line]
                      >>
                      hi eyeryone
                      001101000000000 000100000000000 000 is a number in binary system.how can
                      i get the number of '1's ?
                      thanks in advance
                      >>
                      >You can use a combnation of shifts and bit masks to calculate the number
                      >of set bits. Code for this has often been posted to this group. A
                      >Google search might help.
                      >>
                      the idea is great. and it work perfectly. but as i said before ,there
                      is little '1' in the number of binary system. must i get the number of
                      '1's
                      it contains(or includes,i don't know which word to choose) by checking
                      it bit by bit .that's maybe the most fanny as well as most challenging
                      part
                      of this small question
                      I think what you're saying is that you have a binary number with
                      relatively few 1s, and you'd like a way to count the 1s that's more
                      efficient than traversing all the bits.

                      As santosh said, this has been discussed here before. A Google search
                      will likely turn up a number of solutions. Here's one I just found:
                      <http://infolab.stanfor d.edu/~manku/bitcount/bitcount.html(I do not
                      vouch for the accuracy of the information).

                      Incidentally, you should look up the word "fanny" in your English
                      dictionary. I don't know what you're trying to say, but it doesn't
                      mean what you think it means.


                      --
                      Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
                      Nokia
                      "We must do something. This is something. Therefore, we must do this."
                      -- Antony Jay and Jonathan Lynn, "Yes Minister"

                      Comment

                      • Ian Collins

                        #12
                        Re: a fanny question

                        Keith Thompson wrote:
                        >
                        Incidentally, you should look up the word "fanny" in your English
                        dictionary. I don't know what you're trying to say, but it doesn't
                        mean what you think it means.
                        >
                        It doesn't even mean the same in American and British English...

                        --
                        Ian Collins.

                        Comment

                        • CBFalconer

                          #13
                          Re: a fanny question

                          Sjouke Burry wrote:
                          xinxin wrote:
                          >
                          >00110100000000 000010000000000 0000 is a number in binary system.
                          >how can i get the number of '1's ?
                          >
                          Count them.
                          Load the phrase into your text editor. Place the cursor on the
                          first digit. Record the column number shown by the editor as l.
                          Move the cursor to just past the last digit. Record the column
                          number as r. Now count the number of zeroes in the whole number,
                          and record as m. Then solve the equation:

                          n + m = r - l

                          for n. Notice that you can also count 1's and solve for the number
                          of zeroes. Ain't maths wonderful!

                          --
                          [mail]: Chuck F (cbfalconer at maineline dot net)
                          [page]: <http://cbfalconer.home .att.net>
                          Try the download section.

                          ** Posted from http://www.teranews.com **

                          Comment

                          • xinxin

                            #14
                            Re: a fanny question

                            On 4ÔÂ23ÈÕ, ÉÏÎç9ʱ14·Ö, Keith Thompson <ks...@mib.orgw rote:
                            xinxin <jnzhouxin...@g mail.comwrites:
                            On 4ÔÂ23ÈÕ, ÉÏÎç2ʱ03·Ö, santosh <santosh....@gm ail.comwrote:
                            xinxin wrote:
                            >
                            [misleading subject line]
                            >
                            hi eyeryone
                            001101000000000 000100000000000 000 is a number in binary system.how can
                            i get the number of '1's ?
                            thanks in advance
                            >
                            You can use a combnation of shifts and bit masks to calculate the number
                            of set bits. Code for this has often been posted to this group. A
                            Google search might help.
                            >
                            the idea is great. and it work perfectly. but as i said before ,there
                            is little '1' in the number of binary system. must i get the number of
                            '1's
                            it contains(or includes,i don't know which word to choose) by checking
                            it bit by bit .that's maybe the most fanny as well as most challenging
                            part
                            of this small question
                            >
                            I think what you're saying is that you have a binary number with
                            relatively few 1s, and you'd like a way to count the 1s that's more
                            efficient than traversing all the bits.
                            >
                            As santosh said, this has been discussed here before. A Google search
                            will likely turn up a number of solutions. Here's one I just found:
                            <http://infolab.stanfor d.edu/~manku/bitcount/bitcount.html(I do not
                            vouch for the accuracy of the information).
                            >
                            Incidentally, you should look up the word "fanny" in your English
                            dictionary. I don't know what you're trying to say, but it doesn't
                            mean what you think it means.
                            >
                            --
                            Keith Thompson (The_Other_Keit h) <ks...@mib.or g>
                            Nokia
                            "We must do something. This is something. Therefore, we must do this."
                            -- Antony Jay and Jonathan Lynn, "Yes Minister"- Òþ²Ø±»ÒýÓÃÎÄ×Ö -
                            >
                            - ÏÔʾÒýÓõÄÎÄ×Ö -
                            oh, you give me the best solution to my quetion. and maybe i should
                            learn english harder .english is important for a chinese to learn
                            software.

                            Comment

                            • Nick Keighley

                              #15
                              Re: a fanny question

                              On 23 Apr, 01:44, xinxin <jnzhouxin...@g mail.comwrote:
                              the idea is great. and it work perfectly. but as i said before ,there
                              is little '1' in the number of binary system.
                              a "little '1'" is presumably ~0.9 and a "big '1'" ~1.1


                              :-)


                              --
                              Nick keighley

                              Comment

                              Working...