Shop OBEX P1 Docs P2 Docs Learn Events
Boolean? Really? — Parallax Forums

Boolean? Really?

User NameUser Name Posts: 1,451
edited 2015-12-07 23:31 in Propeller 1
Edit: Matter is resolved. Nothing to see here. Please move along.

Could someone explain to me the need for a separate category of boolean instructions and variables? Any variable is either zero or not zero, and on that basis alone is perfectly serviceable for use in conditional branching.[\s]

«1

Comments

  • BeanBean Posts: 8,129
    Because bitwise 1 AND 2 = 0 but boolean 1 AND 2 = 1 (or -1 or what ever TRUE is).

    I think this is what you are asking...

    Bean
  • What is prompting this post? If you are talking about Spin, why bring this up now?
  • Heater.Heater. Posts: 21,230
    Bean has it. There is a lot of scope for confusion if you don't know what the type of your variables is.

    Especially in high level languages like C.
    For me, ANY variable is either zero or not zero,...

    I think you express the problem right there. In that scheme:

    1 is TRUE

    0 is FALSE

    101 is TRUE

    but wait a minute, NOT 101 is 010 which is not zero therefor it's TRUE. So we have NOT TRUE is TRUE!

    All the above in binary and pseudo code. Depending on what language you are using and how it treats TRUE, FALSE, if, and whatever different NOT operators it has you get different results.

    This is why high level languages have types, boolean, integer, float, whatever.











  • potatoheadpotatohead Posts: 10,261
    edited 2015-12-08 04:47
    The boolean true on Propeller is -1, all bits set. This has a lot of advantages over a more simple, non zero = true type scenario as Heater pointed out.

    One of those scenarios is doing conditional math.

    X := 50 & ( y <= 1)

    ( y <= 1) will evaluate to either 0, for false, or -1 for true. The AND operation, &, works with the value 50, resulting in either 0 or 50, due to all bits being set.

    In systems where True is "1", a multiply is generally used, and the P1 has no multiply instruction. However, even when there is a multiply instruction, the -1 form of true is faster and smaller in most cases, due to how the bit operations work.

    If your code is math heavy, you may find conditionals expressed in this way, rather than branches, generate more efficient and smaller code.

    Should you plan to capture this in a variable, or use variables in tandem with booleans and compares, having a variable type helps to insure you are working with the values you think you are.

  • jmgjmg Posts: 15,173
    User Name wrote: »
    Could someone explain to me the need for a separate category of boolean instructions and variables? I come from a C, ASM, and FORTH background and have never needed such a distinction. For me, ANY variable is either zero or not zero, and on that basis alone is perfectly serviceable for use in conditional branching. Meanwhile, IF VAR... and IF !VAR... seem perfectly clear as to intent.

    Many Processor systems use exactly that for Boolean,
    - Memory is cheap, and coders are lazy.
    However, in the world of Embedded control, Boolean can represent a single Flag, or Pin.

    There a separate category of boolean instructions and variables can be very useful.
    Even some ARM cores patch around this, by mapping single bits to memory locations on hardware.
    Proper microcontrollers like the 8051, have special boolean opcodes and can JB/JNB/JBC/CPL 256 separate single bit memory locations.

  • For me, ANY variable is either zero or not zero,

    When that variable is controlling the bits of an IO port you want to be a little more discerning.
  • User NameUser Name Posts: 1,451
    edited 2015-12-07 23:11
    potatohead wrote: »
    The boolean true on Propeller is -1, all bits set.

    I'm glad you mention this. I found this terribly misleading when I first read it in the Propeller Manual many moons ago. I wondered if, for example, I could only test entities that were either 0x0 or 0xFFFFFFFF. It seemed horribly inconvenient and bizarre. Of course a little experimentation revealed it not to be the case.

    But until your post I didn't know why such a mention would even be made in the Propeller Manual. OTOH, I can't imagine writing the result of a boolean operation to a hardware port.

    @everyone who responded: Thank you for your well-considered responses.

    @Seairth: Why not now? Is there a moratorium on questions?

  • jmgjmg Posts: 15,173
    User Name wrote: »
    OTOH, I can't imagine writing the result of a boolean operation to a hardware port.
    Really ? That happens all the time in embedded control... The carry flag is often the result of a boolean operation.



  • Doesn't follow, except perhaps in some sort of crazy diagnostic situation. You wouldn't write the carry flag out without shifting, masking, and/or performing some other bit-wise operation that lines up the carry bit of the CCR with the appropriate bit of the i/o register and ensures no unintended changes.

    OTOH, if it was done 40 years ago and with a PDP-8, I retract everything I just said.
  • jmgjmg Posts: 15,173
    edited 2015-12-08 01:09
    I can code this today, not 40 years ago... :)

    Port.Pin := (Heater < SetPoint);

    That is directly writing the result of a boolean operation to a hardware port.
  • JonnyMacJonnyMac Posts: 9,105
    edited 2015-12-08 01:09
    OTOH, I can't imagine writing the result of a boolean operation to a hardware port.

    Huh? What if I want to clear pins 0 and 1 without affecting any others?
    outa &= $FFFF_FFFC
    

    Toggle a bit from one state to the other?
    outa ^= PIN_MASK
    

    Output seven-segment data to a port:
    outa[SEG_MSB..SEG_LSB] := digit[idx] | DEC_POINT
    

    I could go on but I fear I've belabored the point already.

  • Surely you are both aware of the masking and shifting SPIN is doing for you.

    You might also be familiar with how ARM GPIO pins are written.
  • jmgjmg Posts: 15,173
    User Name wrote: »
    Surely you are both aware of the masking and shifting SPIN is doing for you.

    You might also be familiar with how ARM GPIO pins are written.

    My code was not Spin, and uses no masking or shifting on the MCU it compiles to.

    You were the one who claimed they could not imagine writing the result of a boolean operation to a hardware port.

    You seem to be wanting to re-phrase that, now an example has been provided ?
  • I truly have no idea what your example proves. I wasn't aware I had rephrasing a single thing, either. Would you care to show where I have? Or what your example proves?
  • jmgjmg Posts: 15,173
    Err, My example is doing exactly this :

    "writing the result of a boolean operation to a hardware port."

    I'll let others judge what that proves ;)
  • Heater.Heater. Posts: 21,230
    User Name,
    I can't imagine writing the result of a boolean operation to a hardware port.
    Depends how many bits wide the port is does it not?

    A one bit port can output a high or low, 1 or 0, TRUE or FALSE. The later is a BOOLEAN expression.

    Not so multiple bit wide ports of course.

    The treatment/representation/syntax for these things varies from language to language. It's been know to trip me up on my journey from ALGOL, to CORAL, to PL/M, to C, to Spin, to JavaScript....






  • jmg wrote: »
    Err, My example is doing exactly this :

    "writing the result of a boolean operation to a hardware port."

    Perhaps you could flesh out your code example a bit (no pun intended). Your example wasn't exactly ready for prime time. And I refuse to fill in the blanks for you.

  • Well, if you don't want to use the boolean types, don't.

    You asked why, and a few of us stepped up and provided cases that support the why.

    The one I gave does have code speed and size advantages in SPIN.

    In PASM, it is common to set a long to all ones and use that as the basis for a lot of stuff. Doing that can avoid some operations and when combined with conditional execution, provides a fairly direct mapping to what SPIN would do.

    Of course anyone can debate the worth, and or need. One instruction can be Turing complete, but impractical.

    Like anything, it's there to give you options. Not having it there doesn't make sense as the posts above speak to.

  • Heater. wrote: »
    User Name,
    I can't imagine writing the result of a boolean operation to a hardware port.
    Depends how many bits wide the port is does it not?

    A one bit port can output a high or low, 1 or 0, TRUE or FALSE. The later is a BOOLEAN expression.

    Not so multiple bit wide ports of course.

    The treatment/representation/syntax for these things varies from language to language. It's been know to trip me up on my journey from ALGOL, to CORAL, to PL/M, to C, to Spin, to JavaScript....

    Yes, I guess you could write a one-bit boolean (or one of Chip's -1 booleans) to a one-bit port. Never had the situation arise. Never had a chip with a one-bit port. While I still can't imagine it, I have to point out for jmg's benefit that I've never claimed omniscience. The world is full of crazy/amazing things.

  • As for the 40 years ago bit, you will find a lot of things still make good sense today. On smaller scale devices, it may make a lot more sense.

    Use em, don't use em. When you may encounter trouble, or need options later, it's all still there, no worries.

  • potatohead wrote: »
    Well, if you don't want to use the boolean types, don't.

    You asked why, and a few of us stepped up and provided cases that support the why.

    I should use more smiley faces. Some how I've engendered animosity where none was intended.

    My initial question was ill-conceived...that's why I struck it out a while back.
  • User Name wrote: »
    @Seairth: Why not now? Is there a moratorium on questions?

    Not at all! If it had been someone else that had started this thread, I would have put it down as trolling (since this is a well-worn discussion amongst programmers). But since I don't think that's what this is, it makes me think that there was some unsaid incident that prompted you.

    My apologies if my original reply seemed terse or adversarial. That wasn't my intent.
  • jmgjmg Posts: 15,173
    User Name wrote: »
    jmg wrote: »
    Err, My example is doing exactly this :

    "writing the result of a boolean operation to a hardware port."

    Perhaps you could flesh out your code example a bit (no pun intended). Your example wasn't exactly ready for prime time. And I refuse to fill in the blanks for you.
    Oh dear - my code works exactly as it is written

    I have no idea what "wasn't exactly ready for prime time" or "fill in the blanks" mean, but I do sense a gulf here...

  • Heater.Heater. Posts: 21,230
    So many confusing things here.

    A bit is a low level concept, the smallest amount of information you can have, a single memory cell or I/O path. Values usually referred to as 1 and 0.

    A "boolean" is a high level concept. A type with two values TRUE or FALSE.

    A boolean can be represented by a single bit, 0 and 1. Or many bits 0 and -1, or 0 and something that is not zero.

    A programming language may or may not support boolean types. Or single bit values. A single bit value may or may not be usable directly as a boolean value (Not in Ada for example)

    XMOS xcore chips have one bit wide I/O ports. As physical hardware features and syntactically in their programming language XC.



  • User NameUser Name Posts: 1,451
    edited 2015-12-08 05:29
    Seairth wrote: »
    User Name wrote: »
    @Seairth: Why not now? Is there a moratorium on questions?

    Not at all! If it had been someone else that had started this thread, I would have put it down as trolling (since this is a well-worn discussion amongst programmers). But since I don't think that's what this is, it makes me think that there was some unsaid incident that prompted you.

    My apologies if my original reply seemed terse or adversarial. That wasn't my intent.

    Thank you for your graciousness. You're exactly right - there was a precipitating incident. It stemmed from the fact that SPIN is just a tool to support my PASM habit. And PASM is just a tool to make the world do my bidding. So I never know more SPIN than is necessary. But I know a lot of C because embedded programming was my livelihood for many years.

    Anyway, as Heater suggests, differences between languages can be a source of frustration. Sometimes the differences in terminology are the problem. Sometimes the presentation of the information is the problem. Sometimes the symbols clash. Sometimes my memory fails me. Sometimes I'm too impatient.

    Not running in programming circles (I'm an engineer), I didn't have any idea that this topic had already been beaten to death.

    FWIW, one of the things I love about C is K & R's decision to use the terms "logical operator" and "bitwise operator" and to pair the two families of operators wherever possible (&, && for example). In fact I'm so used to C that its operators are less of a convention and more of a law of nature. :)

  • kwinnkwinn Posts: 8,697
    User Name wrote: »
    Surely you are both aware of the masking and shifting SPIN is doing for you.

    You might also be familiar with how ARM GPIO pins are written.

    Another example in PASM that can shorten the code in a loop.
            ror       Data, #1 wc        ' rotate and set carry if lsb is 1
            muxc      outa, mask       ' output the carry bit state to the pin
    

  • potatoheadpotatohead Posts: 10,261
    edited 2015-12-08 04:10
    :) Just statements of fact. You should have no worries. Text is just hard sometimes.
  • jmg wrote: »
    User Name wrote: »
    jmg wrote: »
    Err, My example is doing exactly this :

    "writing the result of a boolean operation to a hardware port."

    Perhaps you could flesh out your code example a bit (no pun intended). Your example wasn't exactly ready for prime time. And I refuse to fill in the blanks for you.
    Oh dear - my code works exactly as it is written

    Please share with us how you managed that without so much as a cognew or a start.

  • kwinn wrote: »
    User Name wrote: »
    Surely you are both aware of the masking and shifting SPIN is doing for you.

    You might also be familiar with how ARM GPIO pins are written.

    Another example in PASM that can shorten the code in a loop.
            ror       Data, #1 wc        ' rotate and set carry if lsb is 1
            muxc      outa, mask       ' output the carry bit state to the pin
    

    That's pretty ingenious. I'm earmarking that one. Of course you're still selecting output pins with the use of a mask - and that is a bitwise operation. I'm sure jmg's example does that as well. But I'm not setting up his strawmen for him.

  • jmgjmg Posts: 15,173
    User Name wrote: »
    Please share with us how you managed that without so much as a cognew or a start.

    Why would I need a cognew ? as my general posts above said, this is not Spin (nor a Prop) - it is an 8051 target.

    Here is the source line
      P2.3 := (Heater < SetPoint);
    

    and this is the assembler created :
                    MOV     A,Heater                                ; 03C2 E5 76
                    ADD     A,#0ABh                                 ; 03C4 24 AB
                    CPL     C                                       ; 03C6 B3
                    MOV     P2.3,C                                  ; 03C7 92 A3
    

    Most will easily see the "writing the result of a boolean operation to a hardware port" here.

    The muxc example further above, shows the (very similar) Prop1 version of Copy-carry-to-bit :
    MUXC D,S    Copy C to bits in D using S as mask
    MUXNC D,S   Copy !C to bits in D using S as mask
    
Sign In or Register to comment.