Please Explain Count the number of bits in an int code

This is a discussion on Please Explain Count the number of bits in an int code within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Daved >> std::numeric_limits<int>::digits That will return the number of digits that an int can have, not the ...

  1. #16
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Quote Originally Posted by Daved
    >> std::numeric_limits<int>::digits
    That will return the number of digits that an int can have, not the number of bits. You want:
    Code:
    std::cout << std::bitset<sizeof(int)*CHAR_BIT>(your_int_value_here).count() << std::endl;
    I don't remember offhand where CHAR_BIT is defined.
    actually it works correctly, since an int has 31 binary digits.

  2. #17
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,798
    According to the table in my copy of Josuttis's The C++ Standard Library (7th printing, page 61):

    digits: number of nonsigned bits (binary digits)
    digits10: Number of decimal digits

    Yeah, it is misleading, not what I'd expect when I think "digits" in my mind.

    The related/relevant C constants (CHAR_BIT, etc...) are defined in one of the <climits>, <limits.h>, <cfloat>, or <float.h> headers (bottom of page 60).
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #18
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    My mistake, I was confusing digits and digits10.

  4. #19
    Registered User
    Join Date
    Apr 2006
    Posts
    38
    THanks for your replies. Can somebody give me a link of an explenation of &0xf and &0x1? Are there other &0x commands? Does &0x1 give you the first bit? I was never taught any bit manipulation in school and now I'm trying to get some basics down before my January interviews.

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    I did a search of "bit manipulation C++" in a popular search engine... and the first result was an article from cprogramming.com's FAQ. Go figure
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    & is bit and operator - read about bit-wise operations like http://msdn2.microsoft.com/en-us/library/17zwb64t.aspx


    0xf, 0x1 etc is a hex number 0x prefix shows that the number is in the hex
    0,1,2,3,4,5,6,7,8,9,a,b,c,d,f is regular hex digits
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #22
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    Before you learn what they do in things like bit manipulation, it would be good to learn what they actually are.

    They are simply hexidecimal numbers, and nothing more.

    Search on Google or Wikipedia for "hexidecimal" and learn how to count and do math in hexidecimal notation. "0x" is simply a prefix telling the compiler that the text following it will be a hexidecimal number. Then everything following the "0x" is a hexidecimal number.

    After you learn about hexidecimal, you will be prepared to see what usefulness it has in working with and manipulating the bits of any variable.

    [Edit]
    A link: http://en.wikipedia.org/wiki/Hexidecimal
    [/Edit]
    My Website

    "Circular logic is good because it is."

  8. #23
    Registered User
    Join Date
    Apr 2006
    Posts
    38
    I already know all about hex and binary just not what the syntax of the operators are.

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    I already know all about hex and binary just not what the syntax of the operators are.
    Read the FAQ. It will teach you about the syntax.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #25
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    syntax is as for any other binary operator:
    a = b+c;
    a = b&c;
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #26
    Registered User
    Join Date
    Apr 2006
    Posts
    38
    I came up with another solution to the original question. Does anybody see anything wrong with my solution?

    Code:
    int countBits(int x){
      int numBits=0;
      int intSize=sizeof(int)*4;  
      for(int i=0;i<intSize;i++){
        if(x &0x1 == 1)
          numBits++;
        x>>=1;
      }
      return numBits;
    }

  12. #27
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Nothing wrong. It's the most straight-forward solution.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #28
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Why
    int intSize=sizeof(int)*4;?
    Shouldn't it be

    int intSize=sizeof(int)*CHAR_BITS;

    Where 4 is coming from? in most platforms number of bits in char is 8 (and is defined by the constant in the <limits.h>
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  14. #29
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    [CHAR_BITS] is defined [. . .] in <limits.h>
    You mean <climits>.

    Also, I would use sizeof(x), not sizeof(int), in case you change the type of the argument.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #30
    Registered User
    Join Date
    Apr 2006
    Posts
    38
    I should have multiplied by 8 and not 4, my fault.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 07:19 AM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  4. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  5. Replies: 4
    Last Post: 11-23-2003, 06:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21