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; No, you should have multiplied by CHAR_BITS. Check out this thread: http://cboard.cprogramming.com/showthread.php?t=86809...

  1. #31
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    No, you should have multiplied by CHAR_BITS. Check out this thread: unsigned char
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,470
    If you've read the suggested thread you'd knew that not always
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    ZuK
    ZuK is offline
    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,893
    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
    21,401
    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 09:43 PM.
    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

Page 3 of 3 FirstFirst 123
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