Shop OBEX P1 Docs P2 Docs Learn Events
Why is TRUE == -1 — Parallax Forums

Why is TRUE == -1

Jay KickliterJay Kickliter Posts: 446
edited 2008-08-18 23:43 in Propeller 1
In spin, why is TRUE equal to -1? Isn't TRUE usually 1 in most languages?

Also, what is the maximum delay I can create when using waitcnt @ 20MHz? Am I limited to the maximum value of a 32 bit singed character? I ask, because I somehow need to program a ~2 hour delay for a balloon-cutdown program.

Comments

  • AleksAleks Posts: 52
    edited 2008-08-12 13:54
    In most cases, true == 1. However in the spin language, much the same as the subset of Java used on the Javelin stamp, signed integers are used. This uses the most significant bit of the given byte, word, or long to indicate a negative or positive number. Therefore, instead of a word reaching from 0 to 65535, a signed word has a range of -32768 to 32767 (Correct me if I'm mistaken). Thus, since the boolean logic is based on binary values %00000000 and %11111111 when interpreted as an integer it returns 0 (false) or -1 (true).

    As for the maximum delay, you are limited to a 32 bit integer. However, you can stack multiple delays in a loop routine to compensate for this. For instance,
    waitcnt(cnt + clkfreq) will give you a 1 second delay. If you multiply clkfreq by 60 your delay is stretched to a minute : waitcnt(clk + clkfreq*60) If you set this in a loop from 60 times you are left with a 1 hour delay, or if you loop it 120 times you get a 2 hour delay.

    repeat 120
    waitcnt(cnt + clkfreq*60)
    ........

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ~Some men see things as they are and ask "why?"
    I dream of things that never were and ask "why not?"~
  • Mike GreenMike Green Posts: 23,101
    edited 2008-08-12 14:09
    In Spin, false is zero and true is non-zero as far as testing for true and false is concerned. All the conditional statements (IF / REPEAT) will respond to zero / non-zero. There are some advantages for the use of all ones bits for true in that you can use bit-wise logical operators with true to select a value. For example, "(a < 3) & 1234" yields 1234 if a < 3 is true and zero otherwise. In systems where true is a 1, you can do this with a multiply as in "(a < 3) * 1234", but multiply has to be done by subroutine in the Propeller, so it's slower than the logical operators.

    For timing, even though the system clock wraps around at 32 bits, you can wait for longer times with the same accuracy as shorter ones by using multiple WAITCNTs like:
    time := cnt  ' get current time
    repeat 120  ' repeat for a total wait of two hours
       waitcnt(time += clkfreq * 60)  ' wait until one minute in the future
    
  • RaymanRayman Posts: 14,162
    edited 2008-08-12 16:16
    I like to think of it as fuzzy logic, where 0000_0000==false, FFFF_FFFF==true, and anything in between is neither true nor false...·
  • Beau SchwabeBeau Schwabe Posts: 6,559
    edited 2008-08-13 15:38
    "In spin, why is TRUE equal to -1? Isn't TRUE usually 1 in most languages?"

    A TRUE in other languages is also represented by a "-1" ... Take Visual Basic for example...

    http://en.wikipedia.org/wiki/Visual_Basic

    ..."Boolean constant True has numeric value −1.· This is because the Boolean data type is stored as a 16-bit signed integer. In this construct −1 evaluates to 16 binary 1s (the Boolean value True), and 0 as 16 0s (the Boolean value False). "

    In the case of the Propeller, the same thing is true, with the exception that the Boolean numbers are stored as a 32-bit signed integer instead of a 16-Bit signed integer.

    To further quote the Visual Basic wiki site...

    "This definition of True is also consistent with BASIC since the early 1970s Microsoft BASIC implementation and is also related to the characteristics of microprocessor instructions at the time."





    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.

    Post Edited (Beau Schwabe (Parallax)) : 8/13/2008 4:42:26 PM GMT
  • heaterheater Posts: 3,370
    edited 2008-08-13 17:19
    I always liked the idea that if you complement TRUE you get FALSE and if you complement FALSE you get TRUE.
    This works out if FALSE is zero and TRUE is minus one.
    In C and other language any random non-zero value is TRUE and complementing most of them will get you a different non-zero number that is also TRUE. Yuk.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.
  • Timothy D. SwieterTimothy D. Swieter Posts: 1,613
    edited 2008-08-15 00:17
    Jay -

    Are you working on a high altitude balloon project? If so, tell us more. We would love to hear about it. Maybe in another thread though.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Timothy D. Swieter, E.I.

    www.brilldea.com·- Prop Blade, LED Painter, RGB LEDs, uOLED-IOC, eProto fo SunSPOT, BitScope
    www.sxmicro.com - a blog·exploring the SX micro
    www.tdswieter.com
  • Jay KickliterJay Kickliter Posts: 446
    edited 2008-08-15 15:35
    Thanks everyone for clearing it up for me. I think I get it now. I read the Wikipedia article about integers, and two's compliment. But I still don't understand what Heater means by "compliment" in a verb-sense.

    Tim, I am working on a balloon project. But at this point I don't have much substance to be worthy of a new thread/announcement. I need to learn this language a bit more and get my Ham license before launch. It's just me and a buddy working on it, so it's slow going now. What I do know is that although we're using amateur radio equipment for telemetry, this is not going to be a ham-centric project. We want it to be easier for non-ham people to understand and duplicate. I am also using a Motorola c168i phone for text-message position reports, but it is proving to be more difficult than expected. The Open GPS Tracker people have it down, but there's not a whole lot of information on interfacing with this phone outside of their project. I may just be overlooking something simple. I've only been able to get it to send a text message once, connecting to it through a USB serial adapter and my computer. I haven't even tried to get the propeller to communicate with it yet. The c168i phone might warrant a whole thread in itself, as I think it could be of value to propeller projects (since it's only $15, and has a TTL serial port). When I figure something out, I post it.
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2008-08-15 15:39
    Jay Kickliter said...
    The c168i phone might warrant a whole thread in itself, as I think it could be of value to propeller projects (since it's only $15, and has a TTL serial port). When I figure something out, I post it.

    Jay,

    I'd like to hear more about this when you have opportunity..

    OBC

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    New to the Propeller?

    Getting started with a Propeller Protoboard?
    Check out: Introduction to the Proboard & Propeller Cookbook 1.4
    Updates to the Cookbook are now posted to: Propeller.warrantyvoid.us
    Got an SD card connected? - PropDOS
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-08-15 16:03
    Jay,

    What heater meant by "complement" is "to perform the bitwise NOT operation (a.k.a. ones-complement)", wherein each bit is inverted from its current state. To "compliment" an integer would involve saying something nice about it, such as, "Whoa! Nice bits!" smile.gif

    -Phil

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'Still some PropSTICK Kit bare PCBs left!
  • heaterheater Posts: 3,370
    edited 2008-08-16 09:51
    Phil, You explained that so much better than I ever could have!

    Actually, I think I got the spelling correct quite by accident.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.
  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-08-18 23:43
    The history of TRUE and FALSE is that with the high cost of memory in the old days, many computers supported boolean values ie 1 bit, FALSE = 0 TRUE = 1. As memory became cheep, the cost of implementing true boolean became more expensive than using an entire memory location due to the hardware necessary to implement it. At this point False = 0 in the native data width (32 bits in the case of the Propeller) and TRUE = NOT(FALSE), which is 1s in every bit. In 2's compliment all 1s is = -1.

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

    Parallax, Inc.
Sign In or Register to comment.