storing data in bits.

This is a discussion on storing data in bits. within the C Programming forums, part of the General Programming Boards category; Can someone give me some idea on how to store data in 'bit's. I am currently working on bit operators ...

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

    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,259
    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%.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,301
    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
    55
    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
    21,634
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,301
    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
    55
    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, 01: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21