Thread: Array like accss to the bits of a byte

  1. #16
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    Wow, I can't believe discussion on this arose! I would like to apologize for my troll-like behavior towards "Mwuahahaha". Seeing as this type of discussion can only improve my coding, there is no point in attacking someone. Also, remember, this code was written by an online friend of mine. When I first saw it, I thought it was pretty good seeing as I was a complete noob. Heck, I'm still borderline a noob but I've been coding to improve. This guy(my friend whose been programming since he was 7) said that he could write "professional level code". Do you guys consider this "professional level code"? If this code was used in some software for a company, do you think it would work well with everything else?

  2. #17
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I guess the red herring in the bits class (together with its name) is operator[]. This confuses the responsibilities of either class, and allows weirdness like:

    Code:
    twiddler x(8);
    bool b = x[0][3]; //so, which bit do we want to access?
    Good design would strive to make such nonsense a compile-time error (which would be achieved by not overloading [] in bits).

    Another issue I have is with operator* in the twiddler class. Unary * means dereferencing and is there for classes that act like pointers (to dereference an iterator or a smart pointer).

    So yeah, your friend does seem to have a tendency to misname functions/classes and cause confusion.

    OTOH, the code works. This is how you can access bits with the array syntax.
    Last edited by anon; 08-27-2010 at 05:18 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #18
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    Quote Originally Posted by anon View Post
    I guess the red herring in the bits class (together with its name) is operator[]. This confuses the responsibilities of either class, and allows weirdness like:

    Code:
    twiddler x(8);
    bool b = x[0][3]; //so, which bit do we want to access?
    Good design would strive to make such nonsense a compile-time error (which would be achieved by not overloading [] in bits).

    Another issue I have is with operator* in the twiddler class. Unary * means dereferencing and is there for classes that act like pointers (to dereference an iterator or a smart pointer).

    So yeah, your friend does seem to have a tendency to misname functions/classes and cause confusion.

    OTOH, the code works. This is how you can access bits with the array syntax.
    Then what about this:

    Code:
    class twiddler;
    
    class bit {
        uint8_t& value;
        int n;
        friend class twiddler;
        bit(uint8_t& value, int n) : value(value), n(n) {}
    public:
    
        bool operator*() { return (value & (1 << n)) != 0; }
        bool operator=(bool set_value) {
            if(set_value) {
                value |= (1 << n);
            } else {
                value &= ~(1 << n);
            }
        }
    };
    
    class twiddler {
        uint8_t value;
    public:
        twiddler(uint8_t value = 0) : value(value) {}
    
        uint8_t& operator*() { return value; }
        bit operator[](int n) { return bit(value, n); }
    
    };
    Which could be use like so:

    Code:
    twiddler t(8);
    t[3] = false;
    cout << *t[3] << end;

  4. #19
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Overloading operator* is dubious for this purpose. Operators can be overloaded to give classes concice and familiar syntax. Iterators and smart pointers act like pointers, and therefore they can be dereferenced with * just like pointer. In your case, you are just giving your classes unfamiliar syntax (you don't dereference a normal char variable).

    Compare with usage of std::bitset.

    Code:
    #include <iostream>
    #include <bitset>
    
    int main()
    {
        std::bitset<8> bs(15);
        bs[3] = false;
        std::cout << bs[3] << '\n'; //relies on bitset::reference::operator bool
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Byte Array problem
    By OldGit in forum Networking/Device Communication
    Replies: 8
    Last Post: 02-20-2009, 05:45 AM
  2. Passing a byte and its bits to a function
    By rtarbell in forum C Programming
    Replies: 9
    Last Post: 12-04-2008, 09:24 AM
  3. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  4. SDLKey to ASCII without unicode support?
    By zacs7 in forum Game Programming
    Replies: 6
    Last Post: 10-07-2007, 03:03 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM