C++ OR used on strings

13 views
Skip to first unread message

Richard Langner

unread,
Jan 19, 2022, 9:20:11 AM1/19/22
to Sheffield Hardware Hackers
 On this page there is an OR operator which is a single pipe character (|)

  // Copy values from the JsonDocument to the Config
  config.port = doc["port"] | 2731;
  strlcpy(config.hostname,                  // <- destination
          doc["hostname"] | "example.com",  // <- source
          sizeof(config.hostname));         // <- destination's capacity

If the first parameter can't be found it returns the second on these lines
config.port = doc["port"] | 2731;
and
doc["hostname"] | "example.com"

I've not seen this before and wonder why it's not doing a bitwise OR as I would expect from the single pipe (|) character.

Can anyone point me to guidance on this?

Richard



nop head

unread,
Jan 19, 2022, 10:53:42 AM1/19/22
to Sheffield Hackspace
The port is an int, not a string, so it should definitely do a bitwise OR, which is a bug as far as I can see. Perhaps they should be both ||.

--
You received this message because you are subscribed to the Google Groups "Sheffield Hackspace" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sheffield-hardware-...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/sheffield-hardware-hackers/4fb3c542-d6b5-bb54-8156-73edf0097b69%40gmail.com.

Richard Langner

unread,
Jan 19, 2022, 10:57:15 AM1/19/22
to sheffield-har...@googlegroups.com
As I understand it, single pipe is bitwise OR, double pipe is logical OR.


On 19/01/2022 15:53, nop head wrote:
The port is an int, not a string, so it should definitely do a bitwise OR, which is a bug as far as I can see. Perhaps they should be both ||.

On Wed, 19 Jan 2022 at 14:20, Richard Langner <richard...@gmail.com> wrote:
 On this page there is an OR operator which is a single pipe character (|)

  // Copy values from the JsonDocument to the Config
  config.port = doc["port"] | 2731;
  strlcpy(config.hostname,                  // <- destination
          doc["hostname"] | "example.com",  // <- source
          sizeof(config.hostname));         // <- destination's capacity

If the first parameter can't be found it returns the second on these lines
config.port = doc["port"] | 2731;
and
doc["hostname"] | "example.com"

I've not seen this before and wonder why it's not doing a bitwise OR as I would expect from the single pipe (|) character.

Can anyone point me to guidance on this?

Richard



--
You received this message because you are subscribed to the Google Groups "Sheffield Hackspace" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sheffield-hardware-...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/sheffield-hardware-hackers/4fb3c542-d6b5-bb54-8156-73edf0097b69%40gmail.com.
--
You received this message because you are subscribed to the Google Groups "Sheffield Hackspace" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sheffield-hardware-...@googlegroups.com.

Graham Driver

unread,
Jan 19, 2022, 11:37:03 AM1/19/22
to sheffield-har...@googlegroups.com
Google says that | can be used as a non-short-circuiting version of ||. I understand the concept but have no idea about the implications.

In any event I guess the chap is looking for something being false, null, empty or zero. 


From: sheffield-har...@googlegroups.com <sheffield-har...@googlegroups.com> on behalf of Richard Langner <richard...@gmail.com>
Sent: 19 January 2022 15:57
To: sheffield-har...@googlegroups.com <sheffield-har...@googlegroups.com>
Subject: Re: [SHH:17341] C++ OR used on strings
 

Paul M

unread,
Jan 19, 2022, 5:22:36 PM1/19/22
to sheffield-har...@googlegroups.com
Hi Richard,

I guess you didn't spot this bit on that page:

JsonVariant
JsonVariant::operator|

Links to https://arduinojson.org/v6/api/jsonvariant/or/

It's using operator overloading on that class to change what a normal "|" (bitwise or) would usually do.

Regards,
Paul


On Wed, 19 Jan 2022 16:37:01 +0000
Graham Driver <graham...@hotmail.com> wrote:

> Google says that | can be used as a non-short-circuiting version of ||. I understand the concept but have no idea about the implications.
>
> In any event I guess the chap is looking for something being false, null, empty or zero.
>
> ________________________________
> From: sheffield-har...@googlegroups.com <sheffield-har...@googlegroups.com> on behalf of Richard Langner <richard...@gmail.com>
> Sent: 19 January 2022 15:57
> To: sheffield-har...@googlegroups.com <sheffield-har...@googlegroups.com>
> Subject: Re: [SHH:17341] C++ OR used on strings
>
> As I understand it, single pipe is bitwise OR, double pipe is logical OR.
> ________________________________
>
> On 19/01/2022 15:53, nop head wrote:
> The port is an int, not a string, so it should definitely do a bitwise OR, which is a bug as far as I can see. Perhaps they should be both ||.
>
> ________________________________
> On Wed, 19 Jan 2022 at 14:20, Richard Langner <richard...@gmail.com<mailto:richard...@gmail.com>> wrote:
> On this page<https://arduinojson.org/v6/example/config/> there is an OR operator which is a single pipe character (|)
>
>
> // Copy values from the JsonDocument to the Config
> config.port = doc["port"] | 2731;
> strlcpy(config.hostname, // <- destination
> doc["hostname"] | "example.com<http://example.com>", // <- source
> sizeof(config.hostname)); // <- destination's capacity
>
>
> If the first parameter can't be found it returns the second on these lines
>
> config.port = doc["port"] | 2731;
>
> and
>
> doc["hostname"] | "example.com<http://example.com>"
>
>
> I've not seen this before and wonder why it's not doing a bitwise OR as I would expect from the single pipe (|) character.
>
> Can anyone point me to guidance on this?
>
> Richard
>
>
>
> --
> You received this message because you are subscribed to the Google Groups "Sheffield Hackspace" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sheffield-hardware-...@googlegroups.com<mailto:sheffield-hardware-...@googlegroups.com>.
> To view this discussion on the web, visit https://groups.google.com/d/msgid/sheffield-hardware-hackers/4fb3c542-d6b5-bb54-8156-73edf0097b69%40gmail.com<https://groups.google.com/d/msgid/sheffield-hardware-hackers/4fb3c542-d6b5-bb54-8156-73edf0097b69%40gmail.com?utm_medium=email&utm_source=footer>.
> --
> You received this message because you are subscribed to the Google Groups "Sheffield Hackspace" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sheffield-hardware-...@googlegroups.com<mailto:sheffield-hardware-...@googlegroups.com>.
> To view this discussion on the web, visit https://groups.google.com/d/msgid/sheffield-hardware-hackers/CAEEmnrPzcd6T7NAmaGpeU72GDhbS5QKfXS-aOXhVG_-%2B4f1k%2BA%40mail.gmail.com<https://groups.google.com/d/msgid/sheffield-hardware-hackers/CAEEmnrPzcd6T7NAmaGpeU72GDhbS5QKfXS-aOXhVG_-%2B4f1k%2BA%40mail.gmail.com?utm_medium=email&utm_source=footer>.
>
>
> --
> You received this message because you are subscribed to the Google Groups "Sheffield Hackspace" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sheffield-hardware-...@googlegroups.com<mailto:sheffield-hardware-...@googlegroups.com>.
> To view this discussion on the web, visit https://groups.google.com/d/msgid/sheffield-hardware-hackers/4dd3c272-5212-9bd7-1409-9a149c60b06f%40gmail.com<https://groups.google.com/d/msgid/sheffield-hardware-hackers/4dd3c272-5212-9bd7-1409-9a149c60b06f%40gmail.com?utm_medium=email&utm_source=footer>.
>

Richard Langner

unread,
Jan 20, 2022, 12:46:21 PM1/20/22
to sheffield-har...@googlegroups.com
Thanks Paul, I had not seen it at the bottom of the page.
I decided to try out the code in VSC  but the compiler won't let me put an incompatible type or NULL as the first argument, so the code will never get to the default value.

Richard


On 19/01/2022 22:22, Paul M wrote:
Hi Richard,

I guess you didn't spot this bit on that page:

JsonVariant
    JsonVariant::operator|

Links to https://arduinojson.org/v6/api/jsonvariant/or/

It's using operator overloading on that class to change what a normal "|" (bitwise or) would usually do.

Regards,
Paul
Reply all
Reply to author
Forward
0 new messages