Shop OBEX P1 Docs P2 Docs Learn Events
true equals -1? — Parallax Forums

true equals -1?

benben10benben10 Posts: 9
edited 2010-07-06 05:05 in Propeller 1
found this line while going through the Propeller Education Kit in the "Some Operator Vocabulary" section of Lab 4

"A comparison operator returns true (-1) if the values on both sides of the operator make the expression true, or false (0) if the values on both sides make the expression false."

i thought true returns 1.
«1

Comments

  • LeonLeon Posts: 7,620
    edited 2010-07-05 04:08
    The compiler has TRUE pre-defined as -1.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Leon Heller
    Amateur radio callsign: G1HSM
  • benben10benben10 Posts: 9
    edited 2010-07-05 04:17
    what is the reasoning for this?
  • potatoheadpotatohead Posts: 10,261
    edited 2010-07-05 04:29
    Somebody should ask Chip.

    For my own internal sense, I think of it as "completely true". If any part of the value is false, then it's just not true. Yeah, I know STFU, lol! Kind of a zen philosophy thing. A truth is atomic, indivisible. More complex things are actually sums of truths, and unknowns. That's part of my own idea of what truth is. And we operate with a hell of a lot of unknowns, there being very few real truths known to us. So, in a twisted way, I chuckled when I saw the -1 = true on the Prop.

    Honestly, having all the bits set for a true can come in handy for conditional math and the masking that often goes with that. IMHO, it's just a different set of trade offs over the more common 1 = true.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Propeller Wiki: Share the coolness!
    8x8 color 80 Column NTSC Text Object
    Wondering how to set tile colors in the graphics_demo.spin?
    Safety Tip: Life is as good as YOU think it is!
  • LeonLeon Posts: 7,620
    edited 2010-07-05 04:34
    Perhaps it's because one just has to add 1 to make it false, which might simplify things in some cases. In C, 0 is false and every other value is true, which can be a source of confusion.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Leon Heller
    Amateur radio callsign: G1HSM
  • potatoheadpotatohead Posts: 10,261
    edited 2010-07-05 04:44
    You know, that's probably why it was done. One of the design goals for the Propeller and SPIN was to cut down on potential programming errors. Nice observation Leon.

    This turns out to be a difficult subject to search on. Was wondering what other languages use this convention... ???

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Propeller Wiki: Share the coolness!
    8x8 color 80 Column NTSC Text Object
    Wondering how to set tile colors in the graphics_demo.spin?
    Safety Tip: Life is as good as YOU think it is!
  • benben10benben10 Posts: 9
    edited 2010-07-05 04:58
    my friend said that -1 could equal 11 111 111 in a byte. is that what you mean by having all the bits set for use in conditional math?
  • potatoheadpotatohead Posts: 10,261
    edited 2010-07-05 05:07
    Yeah, exactly. A comparison evaluates to either -1 or 0, and can be used with AND, OR, etc... to do things, without using IF - THEN statements. -1 is all bits set in the signed math used on Propeller. Use your windows calculator, set to scientific mode to see this. The windows 7 calculator has the boolean operators too. (sweetness)

    x:=x+((Y>5)&value)
    
    if(y>5)
    x:=x+value
    

    If the comparison is false, the AND operation will result in nothing, X stays the same. If it's true, then all the bits are set, meaning the AND can conditionally add the value to X, all in one nice expression. This kind of thing is useful in the CON section, for example. Really one might want conditionals to determine the constants at compile time. It's necessary to make expressions like this, or do it at run time with the Propeller tool, because the constants are not really executable, just evaluated in sequence. Cluso recently plugged something like this in to his one pin TV driver, so that it could conditionally allocate HUB memory for a video buffer, depending on the text density required on screen. Basically, his approach was to use the HUB target code for the COG, overwriting it once the driver was loaded into the COG and running. Extra memory only needs to be allocated when the video buffer required exceeds the size of the driver footprint in the HUB.

    That was all done with constants and expressions in the code, leaving the user to only define number of characters per line and go.

    Early on, doing that in old BASIC languages that allowed it, sometimes got a person a bit of a speed boost, mostly because getting the work done cost a coupla less tokens. I don't know that it's any faster in SPIN, over just using the IF statement. Probably it is.

    Another thing I like is this is just the kind of thing often done in PASM. It's often handy to have a long somewhere filled with ones, for similar masking, adding etc... With true being -1, the same kind of flow makes sense in both SPIN and PASM.

    Without that, you need a multiply I think:

    x:=x+((Y>5)*value)
    
    The first case sticks with the basics, the second requires a bit more under the hood. Since the Prop does not have a hardware multiply, that's going to be slower.

    Probably somebody is going to express that without a multiply, and that's fine. Just trying to highlight what I was talking about.

    Edit: BTW, long ago this is where I made the association between AND and multiply, and OR with addition. It kind of makes sense in this context, and I've not seen that connection made often. Sorry for all the edits. I kept finding things to plug into this post, and fixing stuff. :)

    Propeller Wiki: Share the coolness!
    8x8 color 80 Column NTSC Text Object
    Wondering how to set tile colors in the graphics_demo.spin?
    Safety Tip: Life is as good as YOU think it is!

    Post Edited (potatohead) : 7/5/2010 5:54:00 AM GMT
  • heaterheater Posts: 3,370
    edited 2010-07-05 05:31
    OK let's see:

    1) The thing about "truth" is that it is "not false".

    2) "False" is represented by "0"

    3) So "not false" is represented "not 0"

    4 ) But "not 0" is "!0" in Spin which is $FFFFFFFF, that is all bits inverted.

    5) And $FFFFFFFF is the representation of -1 in two complement arithmetic.

    6) Therefore "false" is -1.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.

    Post Edited (heater) : 7/5/2010 5:36:09 AM GMT
  • potatoheadpotatohead Posts: 10,261
    edited 2010-07-05 05:34
    [noparse]:)[/noparse] Well played!

    I never did understand the product of a comparison to be signed. It's just bits, on or off.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Propeller Wiki: Share the coolness!
    8x8 color 80 Column NTSC Text Object
    Wondering how to set tile colors in the graphics_demo.spin?
    Safety Tip: Life is as good as YOU think it is!
  • heaterheater Posts: 3,370
    edited 2010-07-05 05:44
    Exactly, seems much more logical than the C idea that false is 0 and true is anything else.

    So for example $AA is true and the inverse of that $55 is also true. Crazy don't you think?

    Has been know to lead to inadvertent bugs in C.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.
  • potatoheadpotatohead Posts: 10,261
    edited 2010-07-05 05:49
    Yeah, I agree. Like everybody else, my initial reaction was "WTF?!?" The first time I wrote a conditional expression, I liked it seeing the nice, easy cheezy AND operator construct being possible. Was an easy sell from there.

    I am curious about other environments where this convention is used. I don't know of any.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Propeller Wiki: Share the coolness!
    8x8 color 80 Column NTSC Text Object
    Wondering how to set tile colors in the graphics_demo.spin?
    Safety Tip: Life is as good as YOU think it is!
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2010-07-05 05:59
    I like that explanation, heater (et al)

    I'm an 8 bit sort of chap, so for me 'true' is 11111111, or 255. Which is -1 in 8 bit numerics.

    The prop is a 32 bit processor, so 'true' on a 32 bit processor is 11111111_11111111_11111111_11111111

    Which you can write as FFFFFFFF or 4294967295. Or -1.

    -1 is quickest to write. And easier to remember than all those decimal digits.

    Hmm - makes sense in my strange warped 8 bit mind!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.smarthome.viviti.com/propeller
  • mparkmpark Posts: 1,305
    edited 2010-07-05 06:33
    heater said...
    Exactly, seems much more logical than the C idea that false is 0 and true is anything else.

    So for example $AA is true and the inverse of that $55 is also true. Crazy don't you think?

    Has been know to lead to inadvertent bugs in C.

    Spin maintains the "non-0 is true" for boolean operators.

    In C, as in Spin, $55 is the inverse of $aa only in the bitwise sense. If you're talking boolean logic, you'd use the logical negation operator: "!0xaa" (in C) or "not $aa" (in Spin) is 0 (aka false).

    I believe most Forths also use -1 for true.
  • heaterheater Posts: 3,370
    edited 2010-07-05 06:59
    Yes indeed mpark. And that's why I've seen so many bugs in C code over the years where "true" "false" and "in between" had been used incorrectly.

    Never did understand why C did not have a boolean type from the get go.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.
  • KyeKye Posts: 2,200
    edited 2010-07-05 14:20
    I'm sorry, having 1 or 0 come out as the result of logical comparisons in C is not very useful. This is a strength of SPIN over C by having -1 or 0 be the result. It requires less code.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,

    Post Edited (Kye) : 7/6/2010 3:57:16 PM GMT
  • jazzedjazzed Posts: 11,803
    edited 2010-07-05 14:31
    Non-zero true is mostly used for passing error codes back to a function caller. The same is done for return codes to an O/S shell for application program exit.

    I see no functional difference between -1 true and not 0 true other than ++true == false. The only bugs I've ever seen caused by the value of true is when someone assumes that it is a specific value like 1 or -1.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Propeller Pages: Propeller JVM
  • localrogerlocalroger Posts: 3,452
    edited 2010-07-05 15:07
    It is standard in high level languages to use all-bits-on as true, so that word-wide logical operators such as AND and OR applied to true and false give appropriate results. With signed math of any width all bits on is -1. Get used to it, you will see this everywhere. Most languages do not provide special non-bitwise logical operators; they use the CPU operators which apply the operation to every bit in the value independently, which gives you NOT $AA = $55.

    The reason for the "not zero = true" convention is that at the CPU level the truthiness check is often carried out by checking the CPU zero flag after an operation. This appropriately detects -1 and 0 as true and false, but it also returns all of the other possible "neither true nor false" values as true.

    Spin is fairly unusual in having special logical operators that force the result to true or false. It's a convenience that would carry noticeable computational costs in a compiled language where performance is critical.
  • Invent-O-DocInvent-O-Doc Posts: 768
    edited 2010-07-05 15:12
    @benben. Thanks for asking the question - a most educational thread.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tom Talbot
    New Market, MD, USA
  • LeonLeon Posts: 7,620
    edited 2010-07-05 16:09
    Interestingly, in Boolean algebra the values are completely arbitrary, and don't even have to be integers. However, it has been shown that arithmetic operations on the set {0, 1} constitute a Boolean algebra, so it looks like 0 and 1 might be the best choice for false and true.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Leon Heller
    Amateur radio callsign: G1HSM
  • jazzedjazzed Posts: 11,803
    edited 2010-07-05 17:52
    The set {0,1} is fine if one wants to remain an academic purist.
    Things get more interesting and malleable in the trenches where
    decisions get made for pragmatic benefit rather than tradition.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Propeller Pages: Propeller JVM
  • RaymanRayman Posts: 14,889
    edited 2010-07-06 00:25
    I would have preferred the C syntax, I think.

    I had some trouble with expressions like: "if i==false", but I don't remember the exact circumstance.

    Seems with Spin, you can have cases where "==false" and "==true" are both false.

    BTW: Visual C++ does have a boolean type.

    Maybe a better question is: Why doesn't Spin have a boolean type?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    My Prop Apps:· http://www.rayslogic.com/propeller/Programming/Programming.htm

    My Prop Info: ·http://www.rayslogic.com/propeller/propeller.htm
    My Prop Products:· http://www.rayslogic.com/Propeller/Products/Products.htm
  • LeonLeon Posts: 7,620
    edited 2010-07-06 00:31
    {0, 1} is used at the machine code level, of course.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Leon Heller
    Amateur radio callsign: G1HSM
  • localrogerlocalroger Posts: 3,452
    edited 2010-07-06 00:40
    Rayman, in Spin -1 aka $FFFF_FFFF is true and 0 is false. This is also the case in every other language that tries to couple at all closely to the underlying CPU instruction set. CPU logical operators work on all bits independently across a word, so to get true/false operation all the bits have to be the same.

    I am pretty sure Visual Studio implements "booleans" as what they call internally "bool32," which is basically exactly what Spin does coercing true to -1 and false to 0 across a 32-bit value. They just also do type checking to make sure you don't try to perform a normal math operation on a bool or vice-versa. Most languages don't hold your hand that way, and it's your responsibility to make sure that a neither true nor false value like $55 doesn't find its way into what should be a boolean logical operation.

    There is no case in Spin where ==false and ==true are both false. Like all languages that decide based on the CPU Z flag, if you use a result to make a logical decision (such as in an IF statement) if the result is zero it will be regarded as false, and if nonzero (even if not -1) as true. If you are working correctly and using actual boolean sources for the operation you'll get the expected result. But you can also say IF 3 THEN ... and since 3 is a valid value that isn't zero the IF statement will proceed as if it was -1 / true.
  • localrogerlocalroger Posts: 3,452
    edited 2010-07-06 00:46
    @Leon, re: {0, 1} is used at the machine code level, of course.

    NO IT ISN'T. STOP TELLING PEOPLE THIS. -1 for true is the standard across the board. Even CPU's that implement one-bit variables for this purpose generally coerce them to -1 before sending them to the ALU. -1 is not some weirdness Chip Gracey invented along with hubs and cogs, it's the way things are done IN EVERY OTHER ARCHITECTURE. You have to understand this if you're going to work in software at all because it is not an exception, it is the rule. All bits on is true, all bits off is false, so that you can use logical operators to do logic on them. But it's up to you as a programmer not to feed an "illegal" value like 4 to an operator like AND, because the compiler doesn't know that you aren't trying to extract bit %0100 with that operation.
  • potatoheadpotatohead Posts: 10,261
    edited 2010-07-06 01:02
    I think your last few contributions have been very educational for me. Don't know about the others. Thanks.

    IMHO, the non-hand holding is the better way. Like anything cool, you can do great things --or make one hell of a mess.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Propeller Wiki: Share the coolness!
    8x8 color 80 Column NTSC Text Object
    Wondering how to set tile colors in the graphics_demo.spin?
    Safety Tip: Life is as good as YOU think it is!
  • LeonLeon Posts: 7,620
    edited 2010-07-06 01:32
    Operations on {0, 1} are how computers work. That is what I meant.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Leon Heller
    Amateur radio callsign: G1HSM

    Post Edited (Leon) : 7/6/2010 2:34:38 AM GMT
  • mparkmpark Posts: 1,305
    edited 2010-07-06 02:13
    localroger said...

    There is no case in Spin where ==false and ==true are both false.

    "42 == false" is false.
    "42 == true" is false.
  • localrogerlocalroger Posts: 3,452
    edited 2010-07-06 02:40
    OK, mpark, ya got me. There's no case yadda yadda yadda if you feed it -1 or 0, which is what all normal environments expect you to do.
  • benben10benben10 Posts: 9
    edited 2010-07-06 03:41
    i had a feeling this might happen, but its a good thing and a good read. looks like i didnt say thank you yet. so, thank you for all the info.
  • potatoheadpotatohead Posts: 10,261
    edited 2010-07-06 04:25
    I learned some new stuff on this one. That's just how it goes when we've got lots of brains in one place! Could easily be worse.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Propeller Wiki: Share the coolness!
    8x8 color 80 Column NTSC Text Object
    Wondering how to set tile colors in the graphics_demo.spin?
    Safety Tip: Life is as good as YOU think it is!
Sign In or Register to comment.