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

bitwise operation

hello,
i m implementing a pc keyboard logic.
pc keboard signals are transmitted serially to port pin.
i want to know how to do bitwise shifting in 'c' for reception as well as for transmission to get the data present on port pin.
i did it in assembly using RRC A.
can i used accumulator and carry flag directly in 'c'?
my code is proper but i hav problem in data management
thank u all.

  • Pick up a decent book on the C language and/or get the language standard.

    No rotate in the language. Possibly in the runtime library for the specific compiler.

    Shift built into the language.

    No access to any carry or shifted-out bits unless you use assembler.

    a <<= 1; // shift a one step left
    a = a << 1; // long form of above
    a >>= 1; // shift a one step right
    a = a >> 1; // long form of above
    a = (a << 1) | 1; // shift in a new bit at low side
    a = (a >> 1) | 0x80; // shift in a new bit to the right (8-bit logic)
    if (a & 1) ; // test if bit is set
    

    Most serial communication is done using hardware devices in the chips, i.e. without the need to shift any bits. Different chips have different hardware for asynchronous or (in this case) synchronous data transfers built in. Receiving serial data in software requires that your program is ready for every single bit. You must either poll the clock signal contnuously or use an interrupt.

  • Keil C51 has intrinsic library functions for rotate. See _crol_, _cror_, _iror/l_, _lror/l_.

    Take another look at your 8051 architecture manual. Port pins are generally bit-addressable. (SFRs whose addresses end in x0 or x8 are bit-addressable.) You can test and set the bits directly, without having to rotate a byte into the carry. Instead of doing a rotate, branch if carry, simply do an "if (bit)".

    It's possible, but unwise, to directly access CPU registers such as the ACC or CY from C. The code generator allocates and uses those registers for its own purposes, and you have no guarantees on its behavior. Even if you peek at the generated code, there's no assurance the pattern will remain exactly the same in the next version of the compiler, or even if you change the source.

    On the bright side, you generally don't need to access those registers from C anyway.