Is there a function that returns the number of bits used to represent an integer?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rohan1987
    New Member
    • Dec 2009
    • 1

    Is there a function that returns the number of bits used to represent an integer?

    Is there a function similar to the sizeof() function used with strings that returns the number of bits used to represent an integer?
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    When you say "... represent an integer", do you mean the int type? If so, then what you want is
    Code:
    sizeof(int) * 8
    (I realize this snippet makes the implementation-dependent assumption that sizeof returns the size in 8-bit bytes.)

    If instead you want to know the minimum number of bits needed to represent some particular integer value, say "452", then you're out of luck. You will have to write your own bit-counting function to do that.

    Comment

    Working...