Thread: Please Explain Count the number of bits in an int code

  1. #31
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    No, you should have multiplied by CHAR_BITS. Check out this thread: http://cboard.cprogramming.com/showthread.php?t=86809
    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.

  2. #32
    Registered User
    Join Date
    Apr 2006
    Posts
    38
    sizeof returns the number of bytes and there are always 8bits to a byte.

  3. #33
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    If you've read the suggested thread you'd knew that not always
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #34
    Registered User
    Join Date
    Apr 2006
    Posts
    38
    I did read the thread and didn't see anything about non 8 bit bytes.

  5. #35
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by dnysveen
    I did read the thread and didn't see anything about non 8 bit bytes.
    Read it again.
    Kurt

  6. #36
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    1) sizeof doesn't return the number of bytes, it returns the multiples of char's size.
    2) Even if it did, nowhere in the C++ standard does it say that a byte is 8 bits large. Nor does it anywhere in computer theory. This is a usage based solely on the fact that it is so common.
    3) Neither is the size of char guaranteed anywhere; there's only a minimum specified.
    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

  7. #37
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    1) sizeof doesn't return the number of bytes, it returns the multiples of char's size.
    hmm... but the C++ Standard (section 5.3.3) states that:
    The sizeof operator yields the number of bytes in the object representation of its operand.

    In the same paragraph, it also states:
    sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1; the result of sizeof applied to any other fundamental type (3.9.1) is implementation-defined.

    I believe this means that a char is exactly one byte in C++, though of course how large is a char or a byte in terms of bits is only given as a minimum.
    Last edited by laserlight; 12-23-2006 at 10:43 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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, 08: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, 07:15 AM