Longer Bit Contants, Please

In the interest in saving memory, time, and readability, could we please have the ability to us bit constants with longs and ints, with the option of having nibble spaces?

It would be great if it could read to the semicolon.

// Number Encoding  ---- Mask ----------------------------- Data to Expect ----------------
const int binary =      0x0000; // 0000 0000 0000 0000; // Standard Binary Numbers
const int greycode =    0x0020; // 0000 0000 0010 0000; // Gray-code Numbers
const int reedsoloman = 0x0040; // 0000 0000 0100 0000; // Reserved for Reed–Solomon

// Number Encoding    ---- Mask ----------------------------- Data to Expect ----------------
const int binary        = 0b 0000 0000 0000 0000; // Standard Binary Numbers
const int greycode      = 0b 0000 0000 0010 0000; // Gray-code Numbers
const int reedsoloman   = 0b 0000 0000 0100 0000; // Reserved for Reed–Solomon

Thank you for your consideration,
BrendaEM

Hi @BrendaEM

I see you have chosen the “Arduino IDE 2.0 beta” forum category, but it’s not clear how this request is related to the Arduino IDE 2.0 beta. Would you mind helping me to understand the request better?

What are “bit constants”? I’m not familiar with this term and I didn’t find anything on Google.

So far as I know, there is no problem with using longs and ints with the hex literal and binary literals I see your snippets.

Is this something supported by C++? If not, then it won’t ever be supported by Arduino.

Just like for octal, decimal, and hexadecimal literals (010, 10, 0x10) you can’t put spaces in the middle of a number.

It should work fine if you take out the spaces.

// Number Encoding    ---- Mask ----------------------------- Data to Expect ----------------
const int binary        = 0b0000000000000000; // Standard Binary Numbers
const int greycode      = 0b0000000000100000; // Gray-code Numbers
const int reedsoloman   = 0b0000000001000000; // Reserved for Reed–Solomon

If you like to group bits into sets of four, use hexadecimal.

Pert and Jonwasser, Thank you for your speedy replies. : )

I wish it allowed spaces up to the semicolon, so I could use nibbles.
It looks like I will have to keeping mapping out the bits and converting the hex values.

You could always write yourself a function:

// Number Encoding    ---- Mask ----------------------------- Data to Expect ----------------
const int binary        = BINARY("0000 0000 0000 0000"); // Standard Binary Numbers
const int greycode      = BINARY("0000 0000 0010 0000"); // Gray-code Numbers
const int reedsoloman   = BINARY("0000 0000 0100 0000"); // Reserved for Reed–Solomon

It might even be possible to write it as a macro so the strings of characters can be optimized away. That is beyond my current knowledge of exotic macro features.

Nibbles_uint(0001, 1010, 1111, 0010)
should be really easy, as long as you don’t use less than 4 digits for any of the nibbles…

#define nibbles_int(a,b,c,d) 0b##a##b##c##d

(and similar for long…)

If it is just readability, C++14 (the Arduino IDE is at level C++12) allows a quote (’) to separate digits:

byte myByte = 0b0101'1101 ; 

https://en.cppreference.com/w/cpp/language/integer_literal

I’m not sure about that. 0010 might be confused with 8.

Oops. I must study the preprocessor a bit better.

Nope, the arguments are apparently passed as text. I just tried it and:
Serial.println(nibbles_uint(0001, 1010, 1111, 0010), HEX);
printed out 1AF2

OK. Thanks. I am no where near wizard level in this pre-processor trickery.
I guess it is this: Token-pasting operator (##) | Microsoft Docs

Johnwasser, Westfw, those option, but what I am after is code-readability.
6v6gt, See, that is why I want the binary option, so I wouldn’t have to do a cut and paste job.

BTW this fails:
byte myByte = 0b0101’1101 ;

Crap:2:21: error: missing terminating ' character
 byte myByte = 0b0101'1101 ;
                     ^~~~~~~ 
Crap:7:1: error: expected ',' or ';' before 'void'
 void setup() {
 ^~~~
exit status 1
missing terminating ' character

Please allow us to use space delimiters between our nibbles .
Thank you, all.

You were told that it would.

In other words, the compiler used by Arduino supports Version 12 of the C++ language and the feature you want is in Version 14.

If you want it readable, use hex.

To the limit of my knowledge, the Burroughs Medium Systems main frame computers were the only ones to use nibbles as the smallest unit of memory.
Paul

What you’re asking to be changed is part of the C/C++ language specification, which is (far) outside of the ability of Arduino to do anything about.

No. In no way did I ask for the C/C++ Specification to be changed.

What I have asked, amounts to adding a few lines of Arduino IDE pre-processor code, so that users can more-easily conserve precious microctontroller memory.

If that threaten everything your know about programming, I am sorry.

What the code would do during compiling is: remove the spaces between the binary constant operator, and the first semicolon it sees.

Also, you may notice that Arduino’s language deviates greatly from C/C++, with many additions pertaining to hardware, but in other ways such not requiring pointers, and the use of Const over the preprocessors DEFINE, as used for the Arduino’s semi-Harvard optimization.

None of those are “deviations” from C/C++. Allowing spaces in numeric constants would be…

remove the spaces between the binary constant operator, and the first semicolon it sees.

It’d be a lot more complicated than that.
output = (flags & 0b1000 1000) ? "bits 7;3 are set" : "not set";

If I were programming the preprocessor…when saw the binary operator, I would seek the next semicolon delimiter, and loop back, cascading, and shifting the characters to the left, eliminated the spaces–until the semicolon delimiter is seen again, and then update the line’s character delimiter.

It’s only complicated if you need it to be. Thepointtothisthreadwasinincreasecodereadability.

[This may or may not be correct that 0b might only take 8-bits.
Arduino - IntegerConstants]

0b is a gcc (and many others) extension of C, and should work up through 64bit constants.

Bxxxxxxx are a set of macros provided by the Arduino team (before they noticed 0b, I think), and they only go up through 8 bits.

I am getting some bus transceivers, and thought about parallel buses.

I am not the only person who separates their nibbles.

JohnWasser suggested a function, and I posted a macro that does something, but you haven’t really weighed in on whether those are acceptable or not…

With C++14 (which Arduino is currently NOT using, you could use a constexpr function that would allow:

  printf("%x\n%x\n%x\n%x\n",
         B("0000 1000 0100 0010"),
         B("1100 1010 0101 1111"),
         B("1111 1000 1111 0010"),
         B("0001_0010_0100_1000"));
void Bad_Binary_Constant(void) __attribute__ (( error("") ));

constexpr uint32_t B (const char *s) {
  uint32_t n = 0;
  while (*s) {
    if (*s == '1') {
      n = (n<<1) + 1;
    } else if (*s == '0') {
      n = n<<1;
    } else if (!(*s == ' ' || *s == '_')) {
      Bad_Binary_Constant();
    }
    s++;
  }
  return n;
}