true equals -1?
benben10
Posts: 9
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.
"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.
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Leon Heller
Amateur radio callsign: G1HSM
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!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Leon Heller
Amateur radio callsign: G1HSM
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!
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:
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
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
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!
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.
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!
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
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.
Never did understand why C did not have a boolean type from the get go.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
For me, the past is not over yet.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,
Post Edited (Kye) : 7/6/2010 3:57:16 PM GMT
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
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.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tom Talbot
New Market, MD, USA
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Leon Heller
Amateur radio callsign: G1HSM
Things get more interesting and malleable in the trenches where
decisions get made for pragmatic benefit rather than tradition.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Propeller Pages: Propeller JVM
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Leon Heller
Amateur radio callsign: G1HSM
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.
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.
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!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Leon Heller
Amateur radio callsign: G1HSM
Post Edited (Leon) : 7/6/2010 2:34:38 AM GMT
"42 == false" is false.
"42 == true" is false.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
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!