Thread: Need help understanding bit operation

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    Beaverton, Oregon, United States
    Posts
    176

    Need help understanding bit operation

    Here is a line I am confused about:

    Code:
    *B_CMD = SRCENX | UPDA1 | UPDA2 | PATDSEL | BCOMPEN | BKGWREN | DSTEN;
    I understand that the left side is a pointer assignment. I understand that the right side is some sort of bitwise OR operation. I do not understand what it is meant to assign to the pointer. This has me totally confused. To me it seems like its ORing every bits in multiple values and then assigning end result to the pointer.

    I just need the general understanding. However if someone needs the rest of the code and the specific values assigned to them I can pop that up here if needed.

    Here is the rest of that function

    Code:
    *A1_BASE = (uint32_t)jagscreen;
    *A1_FLAGS = PIXEL8 | XADDPIX | WID320 | PITCH1;
    *A1_PIXEL = (y << 16) | x;
    *A1_STEP = 0x010000 | (uint16_t)-8;
     
    *A2_BASE = (uint32_t)(textfont + (ch << 3));
    *A2_FLAGS = PIXEL1 | XADDPIX | WID8 | PITCH1;
    *A2_PIXEL = 1;
    *A2_STEP = 0x010000 | (uint16_t)-1;
     
    *B_COUNT = 0x080008;
    *B_PATD = 1;
    *B_DSTD = 0;
    *B_CMD = SRCENX | UPDA1 | UPDA2 | PATDSEL | BCOMPEN | BKGWREN | DSTEN;
    Last edited by A34Chris; 10-15-2014 at 02:37 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Such code is usually used when you want to pack multiple values into a single integer type: char, short, int, etc. It's common, for example, in lower level code like drivers, where many hardware registers use a similar mechanism; and in microcontrollers with limited resources since you don't want to use an entire int for each option when just 1 bit will do.

    It might make more sense if you looked at the definitions for SRCENX, UPDA1, UPDA2, PATDSEL, etc and wrote out their binary values. You'll probably notice that there are no overlapping bits in any of them. So for example, you might see
    Code:
    #define SRCENX 0x01   // 0000 0001
    #define UPDA1 0x02    // 0000 0010
    #define UPDA2 0x04    // 0000 0100
    #define FOO 0x08      // 0000 8000
    #define PATDSEL 0x10  // 0001 0000
    ...
    Often, these values represent individual options for something. They are not mutually exclusive, and you want to be able to set any combination of them. Thus, you bitwise-OR each option you want to set to 1. You can set a bit to 0 by bitwise-ANDing it with it's bitwise inverse:
    Code:
    *B_CMD &= ~FOO;  // disable foo option by setting it's bit to 0
    Or you can toggle it by using XOR.

    Sometimes you'll also see something similar, except an option will have multiple bits set. For example, an option that uses 2 bits can support up to four values. You'll often see something like this
    Code:
    #define BAR 0xC0       // 1100 0000
    #define BAR_VAL0 0x00  // 0000 0000
    #define BAR_VAL1 0x40  // 0100 0000
    #define BAR_VAL2 0x80  // 1000 0000
    #define BAR_VAL3 0xC0  // 1100 0000
    In that case, you would clear the bits then OR in the value you want.
    Code:
    cmd &= ~BAR;  // clear BAR bits
    cmd |= BAR_VAL1;  // set BAR option to VAL1
    Hope that's clear enough.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Location
    Beaverton, Oregon, United States
    Posts
    176
    thank you very much. So this produces one value out of all that bitwise ORing and assigns it to the pointer. In this case it is the new memory location to point at? *pointer means memory address to point to and &pointer means value stored at that location if my memory is correct.

    Thanks again for your help.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You're not assigning the value to the pointer, you're assigning the value to the address where the pointer is pointing. It looks like this is probably happening inside a function where you have passed the variables by pointer so the changes are reflected in the calling function. See this link.


    Jim

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by A34Chris View Post
    In this case it is the new memory location to point at? *pointer means memory address to point to and &pointer means value stored at that location if my memory is correct.
    I think your memory is failing you here. There's a few different aspects of * and & when working with pointers. First, let's start with defitions

    & is the address-of operator. It gives you the address of the object on it's right. It only makes sense to apply this to a variable or function, which have actual addresses in memory. If you use & on a variable of type foo, the result is of type "pointer to foo".

    * is the dereference operator. It gives you the contents of the memory location on it's right. If you use this on a variable of type "pointer to foo", the result is of type "foo".

    * is also use in declarations to signify that variable is a pointer to the aforementioned type.

    Declarations
    Code:
    // a and c are pointers to int, b is a plain int -- notice that the * applies only to an individual variable, not the whole line.
    // It doesn't make sense to use & in a declaration.
    int *a, b, *c;
    
    // a is a pointer, pointing to (storing the address of) b
    a = &b;
    // we store 42 in the memory area pointed to by a -- i.e. we store 42 in b
    *a = 42;
    
    // this makes no sense and is not valid C -- you can't dereference something that isn't a pointer
    *b = 42;
    // c doesn't point anywhere, so you're trying to write (assign) to invalid memory -- this is undefined behavior
    *c = b;
    // c is of type pointer to int; so it a, thus &a is of type pointer to pointer to int -- those types are not compatible
    c = &a;
    // again, c and a are of type pointer to int; *a is of type int -- those types are also not compatible
    c = *a;
    EDIT:
    It has nothing to do with pointers or the like, but & is also the bitwise AND operator and * is also the multiplication operator. However, in these contexts, they are binary operators, requiring two operands (x & y; v *w). In the pointer context, they are unary operators, having only one operand, which is on their right.
    Last edited by anduril462; 10-15-2014 at 05:37 PM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Location
    Beaverton, Oregon, United States
    Posts
    176
    Thanks guys. My memory is faulty I guess. Been a while since I dug into all this. Will study up on this.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Location
    Beaverton, Oregon, United States
    Posts
    176
    Nevermind. Stupid question.

    Thanks everyone for the help!
    Last edited by A34Chris; 10-31-2014 at 06:20 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem understanding simple increment operation
    By Avenger625 in forum C Programming
    Replies: 4
    Last Post: 01-27-2013, 02:04 PM
  2. logical operation
    By siperi in forum C Programming
    Replies: 13
    Last Post: 11-08-2010, 10:28 AM
  3. Mathematical Operation
    By madahmad1 in forum C Programming
    Replies: 3
    Last Post: 08-18-2008, 10:46 AM
  4. new operation
    By NewGuy100 in forum C Programming
    Replies: 3
    Last Post: 07-28-2005, 10:45 AM
  5. Jamsan(or anyone else)-Operation of VC++6.0
    By 7stud in forum C++ Programming
    Replies: 4
    Last Post: 04-12-2003, 08:50 PM