Change a variable to the opposite?


Closed Thread
Results 1 to 11 of 11
  1. #1
    Join Date
    Sep 2007
    Posts
    59

    Default Change a variable to the opposite?

    I want to connect a BCD thumbwheel to my board. However, instead of 1001 = 9 which is what the thumbwheel outputs, I need 0110.

    I'm not sure why the circuitry is like this but I was wondering if there was a simple command to flip a whole variable or what the best way to do this would be?

    Thank you.

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,517


    Did you find this post helpful? Yes | No

    Default Re: Change a variable to the opposite?

    Untested, but give it a try:
    Code:
    For i = 0 to 3
      BCD_Value.0[i] = ~BCD_Value.0[i]
    NEXT
    /Henrik.

  3. #3
    Join Date
    Sep 2007
    Posts
    59


    Did you find this post helpful? Yes | No

    Default Re: Change a variable to the opposite?

    I came up with:

    For I = 0 to 3
    if BCD_In.0[I] = 0 then
    BCD_In.0[I] = 1
    else
    BCD_In.0[I] = 0
    endif
    next I

    But yours is much simpler!
    Perfect.
    Thank you.

  4. #4
    Join Date
    Nov 2009
    Location
    London
    Posts
    251


    Did you find this post helpful? Yes | No

    Default Re: Change a variable to the opposite?

    Code:
    BCD_Value=BCD_Value ^ $0F
    Last edited by Megahertz; - 19th June 2012 at 23:43. Reason: Code Tags

  5. #5
    Join Date
    Sep 2007
    Posts
    59


    Did you find this post helpful? Yes | No

    Default Re: Change a variable to the opposite?

    Megahertz,

    If I understand yours correctly, you're raising the value to the power of 15? How did you come up with 15? Is that a function of the 4 bits? So if it is an 8 bit variable what would the value be?

    I just want to understand the logic behind it.
    Thank you

  6. #6
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343


    Did you find this post helpful? Yes | No

    Default Re: Change a variable to the opposite?

    That is an "exclusive OR" (XOR) with a byte of 00001111. So when you input 00001001, it will return 00000110.

    Here is the truth table on page 79 of the manual.
    Code:
    A    B   A ^ B
    0    0      0
    0    1      1
    1    0      1
    1    1      0
    If only A or only B is 1, then result is 1.
    The ^ operator is commonly used to invert selected bits:
    Last edited by SteveB; - 20th June 2012 at 04:54.

  7. #7
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,611


    Did you find this post helpful? Yes | No

    Default Re: Change a variable to the opposite?

    Hi,

    ...

    Holy Manual ... $ 3.1.14 ...

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  8. #8
    Join Date
    Nov 2009
    Location
    London
    Posts
    251


    Did you find this post helpful? Yes | No

    Default Re: Change a variable to the opposite?

    That is an "exclusive OR" (XOR) with a byte of 00001111. So when you input 00001001, it will return 00000110.

    Here is the truth table on page 79 of the manual.
    Code:
    A    B   A ^ B
    0    0      0
    0    1      1
    1    0      1
    1    1      0
    If only A or only B is 1, then result is 1.
    The ^ operator is commonly used to invert selected bits:
    This would my my answer as well.

  9. #9
    Join Date
    Sep 2007
    Posts
    59


    Did you find this post helpful? Yes | No

    Default Re: Change a variable to the opposite?

    Doh! Since you posted the truth table, you are correct that I did not RTFM. Before my original post I tried to but didn't know where to look. Now I do and since most of the code uses binary I used:
    BCD_In = BCD_In ^ %11111111

    Thank you for the pointers and help!

  10. #10


    Did you find this post helpful? Yes | No

    Default Re: Change a variable to the opposite?

    Is BCD value 9 the only value that needs translation? Could you actually need a BCD to grey code translation?
    Tim Barr

  11. #11
    Join Date
    Oct 2004
    Posts
    448


    Did you find this post helpful? Yes | No

    Default Re: Change a variable to the opposite?

    If your variable is always going to be 4 bits, the complement would be got by variable(comp) = 15-variable.

    Regards,

    Anand

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts