Results 1 to 13 of 13

Thread: I don't want to waste memory

  1. #1

    Thread Starter
    Lively Member dubae524's Avatar
    Join Date
    Jun 2001
    Location
    Currently on this Super Star Destroyer being telekinetically strangled to death by Darth Vader
    Posts
    78

    I don't want to waste memory

    Hey everybody! I've got a question: You see, I only want to use 2 bits for a program. The first thing I did was create a 2-itemed bool array, but I couldn't print it out on the screen properly. So does anyone know how to just use a specific number of bits without having to waste the rest? Help would be greatly appreciated. Thanks!
    - Justin Patrick Butler

    Comme je trouve. "As I find."
    - Butler family quote

    Beneficia sumptos procul superant. "The benefits far exceed the costs."
    - Myself

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    You can't only use 2 bits. The memory isn't addressed that way. The smallest variable you can use is a byte.
    Harry.

    "From one thing, know ten thousand things."

  3. #3

    Thread Starter
    Lively Member dubae524's Avatar
    Join Date
    Jun 2001
    Location
    Currently on this Super Star Destroyer being telekinetically strangled to death by Darth Vader
    Posts
    78
    Well, what about the bool type? That only uses one bit.
    - Justin Patrick Butler

    Comme je trouve. "As I find."
    - Butler family quote

    Beneficia sumptos procul superant. "The benefits far exceed the costs."
    - Myself

  4. #4
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    It doesn't. Boolean variables are just typedefs for an integer type, and I have a feeling that's a 32 bit integer. zero is false and anything non-zero is true.

    You can pack 32 bit flags into a 32-bit int if you want, but you can only access/modify those flags using bit-masking. The memory cannot be addressed in bits.
    Harry.

    "From one thing, know ten thousand things."

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You can use a bitset container for bit flags, but it still allocates memory in blocks of 32 bits for faster access.

    And to be serious: there isn't any difference between using 2 bits or 32 bits if you don't do it 2000 times. This is not the '60s. If you do it 2000 times - well then you don't need 2 bits, but 4000 bits .
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    Bool is an enum type, I heard.

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    bool types:
    BOOL is a typdef of windows = int. TRUE is a define for 1, FALSE is 0.
    bool is a native C++ datatype. Size depends on the compiler, usually 32 bit. But a 64-bit gcc version first had a 64-bit bool, then changed it to 8 bit in a later release.
    VARIANT_BOOL is again a typedef of windows to int. In that case, true is -1 (0xFFFFFFFF) and false is 0, as in VB.

    I don't know about others.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Originally posted by transcendental
    Bool is an enum type, I heard.
    Enums are still integers behind the scenes. You have to store the value somewhere, and in most cases that's in a 32-bit variable.
    Harry.

    "From one thing, know ten thousand things."

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    To extend, make no assumptions about the size of anything, except char (which I think is guaranteed to be 1, whether or not it actually takes up 1 byte or not )
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Yep, char is guaranteed to be 1 minimum unit, 8 bits (1 byte) on a pc, but not necessarily on all computers.

    At least I think so.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  11. #11
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I'm not sure of implementation details (such as on systems where you don't have bits and bytes, or if a byte is 10 bits....... ) but I think sizeof(char) is defined in the standard to be "1".

    Anyone got a copy of the standard? I *really* ought to buy myself one at some point
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Yeah, if it wasn't that you have to pay I would have gotten the standard long ago.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  13. #13
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    If it would actually do any good, I'd have got a copy of the standard through work (for work purposes) on expenses, but somehow I doubt anyone round here is actually bothered about following standards and are quite happy to churn out incorrect code...........*sigh*
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width