Need Help in C programming for PIC16F877a

Discussion in 'Programmer's Corner' started by dezmond, Nov 9, 2008.

  1. dezmond

    Thread Starter New Member

    Sep 27, 2008
    3
    0
    Anyone has any idea how to write the coding for an function say... "inverse()" in c programming which will only invert or toggle the 4 lower significant bits (a0~a3) and leave the 4 most significant bits (a4~a7) unchanged?

    e.g.

    porta = 0bx01010000;

    and after the inverse function,

    porta = inverse(porta);

    so porta now is 0b01011111.

    any one have any ideas? I've got a headache busting my brains out on this problem...

    thanks in advance guys...
     
  2. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
  3. n9352527

    AAC Fanatic!

    Oct 14, 2005
    1,198
    4
    Use bitwise XOR.

    inverse = value ^ 0x0F;
     
  4. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    You can also use the shorthand assignment statement:

    x ^= 0x0F;


    hgmjr
     
Loading...