This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

4 bytes ulong variable

Hi All,,

I want to know a few alternate ways of doing the following thing.

code unsigned long longconstant 0x1234;
unsigned long longvariable;

The 'longvariable' gets its value one bit at a time. Suppose it is inside a for() loop and in each pass of the loop a next bit of the 'longvariable' is initialized.

After all the bits of the 'longvariable' gets their values , it is then compared with the 'longconstant'. If both compare equal then an output pin is set.

So thats all. Can anyone suggest how could it be done.. ?? I am sure there would be more than one ways to do this..

If anyone can throw some quick code , that would be great.

Thankzz && Bye
-rocknmoon

  • Start with doing it the ISO C / Keil way:

    const unsigned long code longconstant = 0x1234UL;
    unsigned long      idata longvariable;
    The 'code' Keil extension says, "Put it in with the program instructions" and the const tells the C compiler that no obvious writes to this variable will be allow at compile time. The 'UL' suffix on the constant value tells the compiler it's an unsigned long constant.

    Next a solution:
    bit buildLongOneBitAtATimeAndCompare(void)
    {
        const unsigned long code longConstant = 0x1234UL;
        unsigned long            longVariable;
        unsigned char            bitCount;
    
        longVariable  = 0;
        for (bitCount = 0; bitCount < 32; ++bitCount)
        {
              longVariable <<= 1;
              longVariable  |= r_port1_1;
        }
    
        return longConstant == longVariable;
    }