Help Bitwise program

I'm having trouble starting my assignment because I don't understand bits, shifting, bitwise and so on e.t.c.

Usually I can find samples on the internet but not in this case.

Where and How do I go about writing my code?
________________________________________

Assignment:

A theater stage has a set of computer-controlled lights. There are 16 lights. A theater employee is working the lights, and you can assume that the on/off situation of the lights is entirely dependent on an unsigned short variable in your program. Your program will supply the following menu:

1) turn on all the lights
2) turn on center stage lights (lights 5-10)
3) turn on left stage lights (lights 11-15)
4) turn on right stage lights (lights 0-4)
5) turn off all lights
6) turn off center stage lights
7) turn off left stage lights
8) turn off right stage lights
9) overlay on/off pattern onto light configuration
10) quit the menu

Error check of user input.
_________________________________________


Any advice or help will be greatly appreciated!!!!!!

Wale

Comments

  • : I'm having trouble starting my assignment because I don't understand bits, shifting, bitwise and so on e.t.c.
    :
    : Usually I can find samples on the internet but not in this case.
    :
    : Where and How do I go about writing my code?
    : ________________________________________
    :
    : Assignment:
    :
    : A theater stage has a set of computer-controlled lights. There are 16 lights. A theater employee is working the lights, and you can assume that the on/off situation of the lights is entirely dependent on an unsigned short variable in your program. Your program will supply the following menu:
    :
    : 1) turn on all the lights
    : 2) turn on center stage lights (lights 5-10)
    : 3) turn on left stage lights (lights 11-15)
    : 4) turn on right stage lights (lights 0-4)
    : 5) turn off all lights
    : 6) turn off center stage lights
    : 7) turn off left stage lights
    : 8) turn off right stage lights
    : 9) overlay on/off pattern onto light configuration
    : 10) quit the menu
    :
    : Error check of user input.
    : _________________________________________
    :
    :
    : Any advice or help will be greatly appreciated!!!!!!
    :
    : Wale
    :
    [blue]Define a mask for a bits to be affected. Mask is a value where the bit in question has 1 and others have 0. Example: "lights 0-4" mask will look in binary as: [b]0000 0000 0001 1111[/b] - lowest significant bit has index zero. In hexadecimal it will be [b]0x001F[/b] (replace every 4 bits with its HEX equivalent). Prepare a #define (or constant, if you like these...) to use in the code:[code]
    #define RIGHT_STAGE_MASK 0x001F
    [/code]Now, you can use the mask to set/clear the masked bits in a value, like so:
    [code]
    short int iAllLights = 0xFFFF; // All lights are ON

    iAllLights &= ~RIGHT_STAGE_MASK; // Clear the masked bits (set to zero)
    iAllLights |= RIGHT_STAGE_MASK; // Set the masked bits again into 1s

    // How to check if all masked bits are ON:
    if ((iAllLights & RIGHT_STAGE_MASK) == RIGHT_STAGE_MASK) {
    // Yes, they are all set to 1s
    }

    // How to check if any of masked bits are ON:
    if (iAllLights & RIGHT_STAGE_MASK) {
    // Yes, some of bits are set to 1
    }
    [/code][/blue]
  • : : I'm having trouble starting my assignment because I don't understand bits, shifting, bitwise and so on e.t.c.
    : :
    : : Usually I can find samples on the internet but not in this case.
    : :
    : : Where and How do I go about writing my code?
    : : ________________________________________
    : :
    : : Assignment:
    : :
    : : A theater stage has a set of computer-controlled lights. There are 16 lights. A theater employee is working the lights, and you can assume that the on/off situation of the lights is entirely dependent on an unsigned short variable in your program. Your program will supply the following menu:
    : :
    : : 1) turn on all the lights
    : : 2) turn on center stage lights (lights 5-10)
    : : 3) turn on left stage lights (lights 11-15)
    : : 4) turn on right stage lights (lights 0-4)
    : : 5) turn off all lights
    : : 6) turn off center stage lights
    : : 7) turn off left stage lights
    : : 8) turn off right stage lights
    : : 9) overlay on/off pattern onto light configuration
    : : 10) quit the menu
    : :
    : : Error check of user input.
    : : _________________________________________
    : :
    : :
    : : Any advice or help will be greatly appreciated!!!!!!
    : :
    : : Wale
    : :
    : [blue]Define a mask for a bits to be affected. Mask is a value where the bit in question has 1 and others have 0. Example: "lights 0-4" mask will look in binary as: [b]0000 0000 0001 1111[/b] - lowest significant bit has index zero. In hexadecimal it will be [b]0x001F[/b] (replace every 4 bits with its HEX equivalent). Prepare a #define (or constant, if you like these...) to use in the code:[code]
    : #define RIGHT_STAGE_MASK 0x001F
    : [/code]Now, you can use the mask to set/clear the masked bits in a value, like so:
    : [code]
    : short int iAllLights = 0xFFFF; // All lights are ON
    :
    : iAllLights &= ~RIGHT_STAGE_MASK; // Clear the masked bits (set to zero)
    : iAllLights |= RIGHT_STAGE_MASK; // Set the masked bits again into 1s
    :
    : // How to check if all masked bits are ON:
    : if ((iAllLights & RIGHT_STAGE_MASK) == RIGHT_STAGE_MASK) {
    : // Yes, they are all set to 1s
    : }
    :
    : // How to check if any of masked bits are ON:
    : if (iAllLights & RIGHT_STAGE_MASK) {
    : // Yes, some of bits are set to 1
    : }
    : [/code][/blue]
    :


    Thanks for your response.

    My instructor believes that I'll be making the assignment too hard. She said the key is in the code (attached below). Unfortunately I don't understand it.

    Can you explain what exactly the codes (below) are doing? Thanks in advance.

    /*
    Name:
    Copyright:
    Author:
    Date: 27/02/03 12:32
    Description: Demonstration of Formula to Turn On a Range of Bits
    */

    #include

    main()
    {
    unsigned short a = 32, b = 13, c;
    int numbits = 3, startbit = 2;

    c = a | (~((unsigned short) ~0 << numbits) << startbit);
    printf("c = %hu
    ", c);

    numbits = 3; startbit = 1;
    c = b | (~((unsigned short) ~0 << numbits) << startbit);
    printf("c = %hu
    ", c);
    }

    -------------------------------------------------------

    /*
    Name:
    Copyright:
    Author:
    Date: 27/02/03 12:32
    Description: Demonstration of Formula to Turn Off a Range of Bits
    */

    #include <stdio.h>

    main()
    {
    unsigned short a = 32, b = 19, c;
    int numbits, startbit;


    numbits = 3; startbit = 1;
    c = a & ~(~((unsigned short) ~0 << numbits) << startbit);
    printf("c = %hu
    ", c);
    c = b & ~(~((unsigned short) ~0 << numbits) << startbit);
    printf("c = %hu
    ", c);
    }





  • [b][red]This message was edited by AsmGuru62 at 2003-2-27 15:29:41[/red][/b][hr]
    [blue][b]c = a | (~((unsigned short) ~0 << numbits) << startbit);[/b]
    [code]
    1. (unsigned short) ~0 << numbits
    - '~' inverts all bits of zero (so we get: 1111 1111 1111 1111)
    - shifts left by 'numbits', so the new bits will get zero value.
    with 'numbits'=3 we get: 1111 1111 1111 1000) - we got 3 new
    bits as the lowest ones.

    2. (~(STEP #1) << startbit)
    - inverts our value again giving 0000 0000 0000 0111
    - shifts left by 'startbit'.
    with 'startbit'=1 we will have: 0000 0000 0000 1110

    It is a way to prepare a mask not using a [b]#define[/b],
    but using flexible code, so you can pass different values
    for 'numbits' and 'startbit'.

    3. '|' - this operator will set to one the bits of target.
    [/code][/blue]



  • I been off since Thursday - 2/27/02 (moving) but I really appreciate your time.

    From the code I posted, what roles do the Variables a, b and c play in code?

    Can I replace them with unsigned short stagelights = 0;

    so it be like:

    unsigned short stagelights = 0, a;
    int numbits = 3, startbit = 2;

    //so will the code below make sense?
    a = stagelight | (~((unsigned short) ~0 << numbits) << startbit);

    Once again I appreciate your time.
    printf("c = %hu
    ", c);
  • [b][red]This message was edited by AsmGuru62 at 2003-3-3 14:44:50[/red][/b][hr]
    :
    : I been off since Thursday - 2/27/02 (moving) but I really appreciate your time.
    :
    : From the code I posted, what roles do the Variables a, b and c play in code?
    :
    : Can I replace them with unsigned short stagelights = 0;
    :
    : so it be like:
    :
    : unsigned short stagelights = 0, a;
    : int numbits = 3, startbit = 2;
    :
    : //so will the code below make sense?
    : a = stagelight | (~((unsigned short) ~0 << numbits) << startbit);
    :
    : Once again I appreciate your time.
    : printf("c = %hu
    ", c);
    :
    [blue]If you planning to hold the current status of stage lights in a variable 'stagelight' then this will be better:[/blue][code]
    // Switch ON the lights by following mask: 0000 0000 0001 1100
    stagelight |= (~((unsigned short) ~0 << numbits) << startbit);
    [/code][blue]"var1 |= var2" means "var1 = var1 | var2". It is true for most operators, not only '|'.[/blue]


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories