Thread: byte is equal 8 bits???

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715

    byte is equal 8 bits???

    Hi, I've read in the Standard this:
    "The sizeof operator yields the number of bytes in the object representation of its operand...
    sizeof(char), sizeof(signed char), sizeof(unsigned char) are 1;..."
    I remebered a post a while ago where I read that because of sizeof(char) is 1 that doesn't mena that it is 8 bits and that number of bits in the byte could be variable. Does it mean that number of bits in byte can be 6 for example?

    On my machine sizeof(double) and sizeof(long double) are equal (8) but I suppose that there are different number of bits. Am I right?
    Can we talk about number of bits which specific type occupies in memory, for example int is 32 bit on most machines or maybe it is corectly to say int is 4 bytes on most machines?
    Thanks!

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    The number of bytes that a data type varies based on the system. It is typical that a char is 1 byte, but unicode and multibyte characters exist to support all those other languages that don't use the (roman?) alphabet, and those types are not 1 byte, but a compiler might just call them chars (are there unicode only compilers that default the char to tow bytes out there?)

    A byte is always equal to 8 bits. It will be the same from machine to machine, OS to OS. Some combinations, like long double, don't really do anything meaningful, but if you said double double, then that might be 16 bytes on your system (I've not used a double double, so I don't know for certain).

    Int is almost universally 32 bits, or 4 bytes. On a 64 bit platform, an int might be 64 bits, but I don't have a 64 bit system to even test that theory on...

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >Does it mean that number of bits in byte can be 6 for example?

    No, CHAR_BIT must be at least 8.

    >On my machine sizeof(double) and sizeof(long double) are equal (8) but I suppose that there are different number of bits. Am I right?

    Maybe your implementation simply chooses not to deal with a long double.

    >Can we talk about number of bits which specific type occupies in memory, for example int is 32 bit on most machines or maybe it is corectly to say int is 4 bytes on most machines?

    I prefer to say that an int occupies exactly sizeof int bytes, each of which contains exactly CHAR_BIT 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.*

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    It is typical that a char is 1 byte
    sizeof(char) is ALWAYS 1 byte

    Now there are wchar variables for unicode that could be the same size or larger then char depending on exactly how big a char really is in terms of bits

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  2. SDLKey to ASCII without unicode support?
    By zacs7 in forum Game Programming
    Replies: 6
    Last Post: 10-07-2007, 03:03 AM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  5. error: identifier "byte" is undefined.
    By Hulag in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2003, 05:46 PM