Shop OBEX P1 Docs P2 Docs Learn Events
Variable Roll Over — Parallax Forums

Variable Roll Over

SailerManSailerMan Posts: 337
edited 2006-10-23 17:39 in General Discussion
What is the most efficient way to calculate a byte variable rolling over....

What I mean is... Let's say I have a byte Variable...

I increment it by one and the Value goes from 0 to 1 the difference is +1. The if I decrement by 2. I want to get -1, not 255. How can I make this happen without a bunch of If then statements??



·

Comments

  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-10-20 23:03
    It depends on the platform you are using, but are you aware that in 8 bit signed arithmetic $FF is -1? If you are working with signed 8 bit numbers, the range is -128 ($80) to +127 ($7F), you can't express 255 in an 8 bit signed value.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • metron9metron9 Posts: 1,100
    edited 2006-10-23 17:39
    The 7th bit of the byte will always be 1 for negative numbers 128 thru 255 and 0 for numbers 0 to 127 so checking that bit tells you if its negative or positive
    In assembler the N flag would be set on a decrement from 0 to 255 so branching can be done without an extra compare or if then type statement.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think outside the BOX!
Sign In or Register to comment.