Thread: Quick question...

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Quick question...

    Is there a 1 bit varible type? And I'm not talking 1 byte (8 bit), like char.

  2. #2
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    you can have a single bit in a struct or class as part of a bitfield
    Code:
    class bits
    {
        unsigned int b1 : 1;
        unsigned int b7 : 7;
    };
    note that using a single bit is actually much slower on most machines. why do you want this?
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Queatrix View Post
    Is there a 1 bit varible type? And I'm not talking 1 byte (8 bit), like char.
    Short answer: no.
    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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The smallest addressable piece of memory is a byte. You aren't going to work around it incredibly easily: you could do something like what Chaos Engine showed you, but the other bits are going to be rationed off anyway whether they're used or not used.

  5. #5
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    The reason I was asking, is because I wanted to load a file into an array, that way, I can handle each binary bit with ease. Would Engine's solution work for this?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Would Engine's solution work for this?
    Nope.
    There is no way to determine whether unsigned int b1 : 1; is the MSB or LSB of a char, short or long.

    Bit fields are an internal representation only. You can't use them to access external devices, file formats, message streams etc in any meaningful way.
    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.

  7. #7
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Read a byte and then cycle through its bits with bitwise operators.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    There's vector<bool> (specialized to use just 1 bit, not byte, per element) and bitset<N> (where N is known at compile time).

    Edit: http://www.thescripts.com/forum/thread60019.html
    Last edited by robatino; 05-29-2007 at 09:04 AM.

  9. #9
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    >> Read a byte and then cycle through its bits with bitwise operators.
    That is what I am currently doing, I was just hoping there was a better way.

    However, thanks for the responces all.

  10. #10
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Beware of vector<bool> it is not a normal/proper STL container, the algorithms that work on vectors are not guaranteed to work properly on vector<bool> since it is a specialized template. I ran into this problem before. Google a bit and become aware of the container you are using, since it isn't a traditional vector.

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Queatrix View Post
    >> Read a byte and then cycle through its bits with bitwise operators.
    That is what I am currently doing, I was just hoping there was a better way.
    This is the same thing, only different:
    http://c-faq.com/misc/bitsets.html
    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.*

  12. #12
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    So you wanted to read in from the file 'bit by bit'?

    eh...

  13. #13
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    @Zacs: Yup.
    @Dave: Like the link.
    @Wraithan: Okay, I figured I'm not going to use it anyway.
    Last edited by Queatrix; 05-30-2007 at 06:02 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM