Thread: Number of bits in types

  1. #1
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    Question Number of bits in types

    How correct is this?

    char: 8 bit
    short: 16 bit
    long: 32 bit
    long long: 64 bit
    int: depending on your system (usually 32 bit today)

    float: 32 bit (or depending on your system?)
    double: 64 bit

    This is only something I believe, but don't know. Is it the same in C++ as in C?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    There are exactly CHAR_BIT bits in a byte/char, there are at least 8.
    There are exactly sizeof(type) bytes in a type. [edit]Though whether or not there are padding bits is another matter.[/edit]
    This is the same for both C and C++.

    Everything else you showed may or may not be true -- other than besides the int, those will be the minimums. [edit]And I don't know that the larger floating types are required to be "bigger" or not.[/edit]
    Last edited by Dave_Sinkula; 12-01-2006 at 09:01 PM. Reason: ...
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    .
    Join Date
    Nov 2003
    Posts
    307
    Investigate limits.h -- it shows you some information like CHARBITS (8 bits per char)

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    This how you would check the memory information of your system. It depends really on you machine. But the following shows the information which was taken out from my machine

    Code:
    #include<stdio.h>
    
    int main()
    {
        printf("These are the memory size information on my system\n");
        
        printf("\n Int     : %d bits\n", sizeof(int) * 8);
        printf(" Floart  : %d bits\n", sizeof(float) * 8 );
        printf(" Char    : %d bits\n", sizeof(char) * 8);
        printf(" Double  : %d bits\n", sizeof(double) * 8);
        printf(" Short   : %d bits\n", sizeof(short) * 8);
        printf(" Long    : %d bits\n", sizeof(long) * 8);
        
        getchar();
        return 0;
    }
    
    /* my output
    These are the memory size information on my system
    
     Int     : 32 bits
     Floart  : 32 bits
     Char    : 8 bits
     Double  : 64 bits
     Short   : 16 bits
     Long    : 32 bits
    
    */
    ssharish2005

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > printf("\n Int : %d bits\n", sizeof(int) * 8);
    Read the other posts, 8 is an assumption.
    Use CHAR_BIT
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    ohh sorry, thought that normally it would be 8. dint look at the Daves post. Updated code

    Code:
    #include<stdio.h>
    #include<limits.h>
    
    int main()
    {
        printf("These are the memory size information on my system\n");
        
        printf("\n Int     : %d bits\n", sizeof(int) * CHAR_BIT);
        printf(" Floart  : %d bits\n", sizeof(float) * CHAR_BIT);
        printf(" Char    : %d bits\n", sizeof(char) * CHAR_BIT);
        printf(" Double  : %d bits\n", sizeof(double) * CHAR_BIT);
        printf(" Short   : %d bits\n", sizeof(short) * CHAR_BIT);
        printf(" Long    : %d bits\n", sizeof(long) * CHAR_BIT);
        
        getchar();
        return 0;
    }
    Thanks Salem

    ssharish2005

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    For C89, the most portable way to handle size_t results in printf is to cast to unsigned long and use %lu. And again, I think only the char types are guaranteed to use all the bits as value bits.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Thanks everyone. So a byte isn't necessary 8 bits, that's something new.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  3. Please Explain Count the number of bits in an int code
    By dnysveen in forum C++ Programming
    Replies: 36
    Last Post: 12-23-2006, 10:39 PM
  4. Help counting number of bits set in an integer
    By JayDiddums10 in forum C Programming
    Replies: 5
    Last Post: 12-07-2006, 03:21 PM
  5. Number Types
    By gnu-ehacks in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 12-19-2001, 01:23 AM