Shop OBEX P1 Docs P2 Docs Learn Events
bitwise vice versa? — Parallax Forums

bitwise vice versa?

ArchiverArchiver Posts: 46,084
edited 2001-06-13 20:59 in General Discussion
I have a BS2, and am wondering how you do this:

lets say we have a number %10101010 (170)
what piece of code will make it equal %01010101 (85)

at the moment I simply subtract the number from 255 (255 - 170 = 85),
does the BS2 have a built in function for doing this?

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2001-06-11 11:46
    At 6/11/2001 Monday 06:11 AM, mbarron.geo@y... promulgated:
    >I have a BS2, and am wondering how you do this:
    >
    >lets say we have a number %10101010 (170)
    >what piece of code will make it equal %01010101 (85)
    >
    >at the moment I simply subtract the number from 255 (255 - 170 = 85),
    >does the BS2 have a built in function for doing this?
    The REV function will provide the same result. Check the Stamp Manual for
    details.
    REV %10101010 8
    Hope that helps

    Regards,

    Bruce Bates
  • ArchiverArchiver Posts: 46,084
    edited 2001-06-11 13:18
    Can't tell what you want to do since your example is ambiguous. Bruce
    noticed your examples are reverses of each other and his answer, of course,
    is correct for that situation.

    Not having had enough caffeine yet, I noticed first that the example is
    right shifted so :

    x=%10101010
    x=x>>1

    Would do that.

    But on reflection, I noticed that the result is also inverted:

    x=x^$FF ' if I recall correctly

    Does any of that help?

    Regards,

    Al Williams
    AWC
    * Control 8 servos at once: http://www.al-williams.com/awce/pak8.htm


    >
    Original Message
    > From: mbarron.geo@y... [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=WU7K9mPkZWoJF2nB21uG2Et2qWcwbAHXfEOmyg9l8Fo2qaFxiAwMdi4yeaCCzUnRoEWhiH7GCZbDTvSoiVkW]mbarron.geo@y...[/url
    > Sent: Monday, June 11, 2001 5:11 AM
    > To: basicstamps@yahoogroups.com
    > Subject: [noparse][[/noparse]basicstamps] bitwise vice versa?
    >
    >
    > I have a BS2, and am wondering how you do this:
    >
    > lets say we have a number %10101010 (170)
    > what piece of code will make it equal %01010101 (85)
    >
    > at the moment I simply subtract the number from 255 (255 - 170 = 85),
    > does the BS2 have a built in function for doing this?
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed with. Text in
    > the Subject and Body of the message will be ignored.
    >
    >
    > Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
    >
  • ArchiverArchiver Posts: 46,084
    edited 2001-06-11 17:44
    >lets say we have a number %10101010 (170)
    >what piece of code will make it equal %01010101 (85)
    >at the moment I simply subtract the number from 255 (255 - 170 = 85),
    >does the BS2 have a built in function for doing this?

    or y = ~x

    inverts the state of each bit. But like Al says, the problem is
    ambiguous. We need more examples of what you want to do. For
    example, if your input is,
    x=%00110110 (54)
    what do you want to happen with that?
    y=~x ' gives %11001001
    y=x ^ $ff ' also gives %11001001
    y= x rev 8 ' gives %01101100
    y= x >>1 ' gives %00011011
    y = 255 -x ' gives %11001001 (201)

    -- regards,
    Tracy Allen
    electronically monitored ecosystems
    mailto:tracy@e...
    http://www.emesystems.com
  • ArchiverArchiver Posts: 46,084
    edited 2001-06-11 21:59
    [font=arial,helvetica]In a message dated 6/11/01 5:13:49 AM Central Daylight Time,
    mbarron.geo@yahoo.com writes:


    I have a BS2, and am wondering how you do this:

    lets say we have a number ············%10101010 (170)
    what piece of code will make it equal %01010101 (85)

    at the moment I simply subtract the number from 255 (255 - 170 = 85),
    does the BS2 have a built in function for doing this?



    No.

    [/font]
  • ArchiverArchiver Posts: 46,084
    edited 2001-06-13 19:01
    I have a number of places in my current project where,
    mostly at the bit level, I am using either

    x = ~y ' or
    x = 1 - y ' for byte would be x = 255 - y

    Does anyone have a sense which of the three methods -
    subtractive, inverse, and xor - taxes the processor
    least? How about token storage? Is inverse (~)
    preferable for readability and consistency? Obviously
    this isn't important on a small scale, but I am nearly
    filling two program slots and want to maintain the
    current structure. This requires a lot of math-based
    decisions because of the limitations of PBASIC's IF...
    THEN.

    My favorite line takes a 12-bit pressure transducer
    reading stored in a word, accounts for zero, and
    lights an LED if either one valve is on and the
    pressure has reached the low target, or the other
    valvs is on and the pressure has reached the high
    target (only one valve is ever on at a time):

    AD = AD + 1 ' in case of zero reading, no div by 0
    OUT14 = (OUT12 * (p_low + 1) / AD) + (OUT13 * AD /
    (p_high + 1))
    ' given: OUT12 + OUT13 <= 1

    Bob Pence



    --- Tracy Allen <tracy@e...> wrote:
    > >lets say we have a number %10101010
    > (170)
    > >what piece of code will make it equal %01010101
    > (85)
    > >at the moment I simply subtract the number from 255
    > (255 - 170 = 85),
    > >does the BS2 have a built in function for doing
    > this?
    >
    > or y = ~x
    >
    > inverts the state of each bit. But like Al says,
    > the problem is
    > ambiguous. We need more examples of what you want
    > to do. For
    > example, if your input is,
    > x=%00110110 (54)
    > what do you want to happen with that?
    > y=~x ' gives %11001001
    > y=x ^ $ff ' also gives %11001001
    > y= x rev 8 ' gives %01101100
    > y= x >>1 ' gives %00011011
    > y = 255 -x ' gives %11001001 (201)
    >
    > -- regards,
    > Tracy Allen
    > electronically monitored ecosystems
    > mailto:tracy@e...
    > http://www.emesystems.com
    >
    >
    >
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed
    > with. Text in the Subject and Body of the message
    > will be ignored.
    >
    >
    > Your use of Yahoo! Groups is subject to
    > http://docs.yahoo.com/info/terms/
    >
    >


    __________________________________________________
    Do You Yahoo!?
    Get personalized email addresses from Yahoo! Mail - only $35
    a year! http://personal.mail.yahoo.com/
  • ArchiverArchiver Posts: 46,084
    edited 2001-06-13 20:59
    Hi Bob,

    Nice pressure xducer formula!

    The method using ~ will always be the fastest and require the least
    memory. We can always test out the alternatives by looking at the
    memory map (CTRL-M). Brian Forbe's book has lots of info on the
    length of code for different operations. Since the number of bits
    that the interpreter has to read will almost always dominate the
    speed equation, fewer tokens almost always works out to faster speed.

    -- best regards
    Tracy Allen
    electronically monitored ecosystems
    http://www.emesystems.com
    mailto:tracy@e...



    >I have a number of places in my current project where,
    >mostly at the bit level, I am using either
    >
    >x = ~y ' or
    >x = 1 - y ' for byte would be x = 255 - y
    >
    >Does anyone have a sense which of the three methods -
    >subtractive, inverse, and xor - taxes the processor
    >least? How about token storage? Is inverse (~)
    >preferable for readability and consistency? Obviously
    >this isn't important on a small scale, but I am nearly
    >filling two program slots and want to maintain the
    >current structure. This requires a lot of math-based
    >decisions because of the limitations of PBASIC's IF...
    >THEN.
    >
    >My favorite line takes a 12-bit pressure transducer
    >reading stored in a word, accounts for zero, and
    >lights an LED if either one valve is on and the
    >pressure has reached the low target, or the other
    >valvs is on and the pressure has reached the high
    >target (only one valve is ever on at a time):
    >
    >AD = AD + 1 ' in case of zero reading, no div by 0
    >OUT14 = (OUT12 * (p_low + 1) / AD) + (OUT13 * AD /
    >(p_high + 1))
    >' given: OUT12 + OUT13 <= 1
    >
    >Bob Pence
    >
    >
    >
    >--- Tracy Allen <tracy@e...> wrote:
    >> >lets say we have a number %10101010
    >> (170)
    >> >what piece of code will make it equal %01010101
    >> (85)
    >> >at the moment I simply subtract the number from 255
    >> (255 - 170 = 85),
    >> >does the BS2 have a built in function for doing
    >> this?
    >>
    >> or y = ~x
    >>
    >> inverts the state of each bit. But like Al says,
    >> the problem is
    >> ambiguous. We need more examples of what you want
    >> to do. For
    >> example, if your input is,
    >> x=%00110110 (54)
    >> what do you want to happen with that?
    >> y=~x ' gives %11001001
    >> y=x ^ $ff ' also gives %11001001
    >> y= x rev 8 ' gives %01101100
    >> y= x >>1 ' gives %00011011
    >> y = 255 -x ' gives %11001001 (201)
    >>
    >> -- regards,
    >> Tracy Allen
    >> electronically monitored ecosystems
    >> mailto:tracy@e...
    > > http://www.emesystems.com
Sign In or Register to comment.