Thread: storing data in bits.

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    68

    storing data in bits.

    Can someone give me some idea on how to store data in 'bit's.
    I am currently working on bit operators and have hit a snag.
    For example there a question in my c reference book:

    Store rainbow colors in a char variable named color.
    When user presses 0 violet appears on screen,press 1 indigo appears on screen and so on..

    The easy way is to have a string array and store all the data.
    But how to achieve this using/manipulating bit?

    Thanks in advance,
    Arun

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You need to view a bit as something which is either turned on or off, not in terms of a colour. So, if you want a set of bits to represent colour mixing, one bit might indicate if the colour includes a violet component, another bit might indicate if it contains an indigo component, etc. Then you just need to set a way to turn relevant bits on and off. To render a colour, naturally, you need to determine whether each bit is on or off and (if multiple bits are set) work out what that means (eg what colour do you want rendered if bits for both indigo and violet are turned on?).

    The various bitwise operations (>>, <<, &, |, ~, and ^) in combination can be used to construct techniques to turn particular bits on and off, or detect if they are set. I won't tell you how, since you will learn more by nutting that out for yourself.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Does this help?
    Code:
    // assign colour bits
    #define RED (1<<0)
    #define ORANGE (1<<1)
    #define YELLOW (1<<2)
    
    // add red to the colour
    colour |= RED;
    
    // is yellow present?
    if ( colour & YELLOW )
    
    // no more orange
    colour &= ~ORANGE
    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.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    This sounds to me more like a palette approach is asked for than a flag per colour. I'd therefore just use a lookup table.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    68
    Quote Originally Posted by iMalc View Post
    This sounds to me more like a palette approach is asked for than a flag per colour. I'd therefore just use a lookup table.
    Whats a lookup table?

    So basically we don't or rather cannot assign values or data to bits.
    Just toggle between 0 and 1 and do some operation if any condition is satisfied?

    Check my code please:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int num=0;
        char color;
        color = 64;
        char colormask;
        colormask = 64;
        printf("Enter a number.");
        scanf("%d",&num);
        switch(num)
        {
            case 0: //red
             //colormask = 64; //1000000
            if((color & colormask))
            printf("Red");
            break;
            case 1: //orange
            colormask = 32; //100000 or colormask=colormask >> 1
            color = color >> 1;
            //printf("%d",color);
            if(color & colormask)
            printf("Orange");
            break;
            case 2: //yellow
            colormask = colormask >> 2; // 16
            color = color >> 2;
            if(color & colormask)
            printf("yellow");
            break;
        }
        return 0;
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ak47
    Whats a lookup table?
    An array or similiar construct where the key value that you are searching for is the index, and the corresponding value is the value of the array element.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    E.g. this:
    Code:
    const char *colour[] = {"red", "orange", "yellow", etc etc};
    
    printf(colour[num]);
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    68
    Quote Originally Posted by iMalc View Post
    E.g. this:
    Code:
    const char *colour[] = {"red", "orange", "yellow", etc etc};
    
    printf(colour[num]);
    That's what i meant by using the string array in my first post.
    Also
    Code:
    // add red to the colour
    colour |= RED;
    i did not find the need for doing this operation as i shift color to what ever value i want in every case.
    Which proves to be more efficient?shifting(>>) color for a case or adding (|) to the existing color every time?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How change the bmp bits data?
    By nutzu2010 in forum Windows Programming
    Replies: 1
    Last Post: 09-26-2011, 04:15 AM
  2. Data structure for storing serial port data in firmware
    By james457 in forum C Programming
    Replies: 4
    Last Post: 06-15-2009, 09:28 AM
  3. Replies: 3
    Last Post: 02-26-2008, 02:12 PM
  4. Storing a float in 16 bits
    By kara3434 in forum C Programming
    Replies: 7
    Last Post: 07-31-2007, 01:34 PM
  5. Replies: 3
    Last Post: 04-04-2007, 07:34 PM