How do I put 16 bit numbers into my code in binary form?

I thought writing B1110111111111111 should have worked but it throws this error

Arduino: 1.8.15 (Windows 10), Board: "Arduino Nano, ATmega328P"

C:\Users\*********\Documents\Arduino\synth\synth.ino: In function 'void loop()':

synth:46:12: error: 'B1111111111111111' was not declared in this scope

       case B1111111111111111:

            ^~~~~~~~~~~~~~~~~

C:\Users\*********\Documents\Arduino\synth\synth.ino:46:12: note: suggested alternative: 'B11111111'

       case B1111111111111111:

            ^~~~~~~~~~~~~~~~~

            B11111111

C:\Users\*********\Documents\Arduino\synth\synth.ino:49:7: warning: large integer implicitly truncated to unsigned type [-Woverflow]

       case 1111111111111101:

       ^~~~

synth:53:12: error: 'B1111111111111011' was not declared in this scope

       case B1111111111111011:

            ^~~~~~~~~~~~~~~~~

C:\Users\*********\Documents\Arduino\synth\synth.ino:53:12: note: suggested alternative: 'B11111101'

       case B1111111111111011:

            ^~~~~~~~~~~~~~~~~

            B11111101

synth:57:12: error: 'B1111111111110111' was not declared in this scope

       case B1111111111110111:

            ^~~~~~~~~~~~~~~~~

C:\Users\*********\Documents\Arduino\synth\synth.ino:57:12: note: suggested alternative: 'B11111101'

       case B1111111111110111:

            ^~~~~~~~~~~~~~~~~

            B11111101

synth:61:12: error: 'B1111111111101111' was not declared in this scope

       case B1111111111101111:

            ^~~~~~~~~~~~~~~~~

C:\Users\*********\Documents\Arduino\synth\synth.ino:61:12: note: suggested alternative: 'B11101111'

       case B1111111111101111:

            ^~~~~~~~~~~~~~~~~

            B11101111

synth:65:12: error: 'B1111111111011111' was not declared in this scope

       case B1111111111011111:

            ^~~~~~~~~~~~~~~~~

C:\Users\*********\Documents\Arduino\synth\synth.ino:65:12: note: suggested alternative: 'B11101111'

       case B1111111111011111:

            ^~~~~~~~~~~~~~~~~

            B11101111

synth:69:12: error: 'B1111111110111111' was not declared in this scope

       case B1111111110111111:

            ^~~~~~~~~~~~~~~~~

C:\Users\*********\Documents\Arduino\synth\synth.ino:69:12: note: suggested alternative: 'B11101111'

       case B1111111110111111:

            ^~~~~~~~~~~~~~~~~

            B11101111

synth:73:12: error: 'B1111111101111111' was not declared in this scope

       case B1111111101111111:

            ^~~~~~~~~~~~~~~~~

C:\Users\*********\Documents\Arduino\synth\synth.ino:73:12: note: suggested alternative: 'B01111111'

       case B1111111101111111:

            ^~~~~~~~~~~~~~~~~

            B01111111

synth:77:12: error: 'B1111111011111111' was not declared in this scope

       case B1111111011111111:

            ^~~~~~~~~~~~~~~~~

C:\Users\*********\Documents\Arduino\synth\synth.ino:77:12: note: suggested alternative: 'B01111111'

       case B1111111011111111:

            ^~~~~~~~~~~~~~~~~

            B01111111

synth:81:12: error: 'B1111110111111111' was not declared in this scope

       case B1111110111111111:

            ^~~~~~~~~~~~~~~~~

C:\Users\*********\Documents\Arduino\synth\synth.ino:81:12: note: suggested alternative: 'B01111111'

       case B1111110111111111:

            ^~~~~~~~~~~~~~~~~

            B01111111

synth:85:12: error: 'B1111101111111111' was not declared in this scope

       case B1111101111111111:

            ^~~~~~~~~~~~~~~~~

C:\Users\*********\Documents\Arduino\synth\synth.ino:85:12: note: suggested alternative: 'B01111111'

       case B1111101111111111:

            ^~~~~~~~~~~~~~~~~

            B01111111

synth:89:12: error: 'B1111011111111111' was not declared in this scope

       case B1111011111111111:

            ^~~~~~~~~~~~~~~~~

C:\Users\*********\Documents\Arduino\synth\synth.ino:89:12: note: suggested alternative: 'B01111111'

       case B1111011111111111:

            ^~~~~~~~~~~~~~~~~

            B01111111

synth:93:12: error: 'B1110111111111111' was not declared in this scope

       case B1110111111111111:

            ^~~~~~~~~~~~~~~~~

C:\Users\*********\Documents\Arduino\synth\synth.ino:93:12: note: suggested alternative: 'B01111111'

       case B1110111111111111:

            ^~~~~~~~~~~~~~~~~

            B01111111

exit status 1

'B1111111111111111' was not declared in this scope



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

0b1110111111111111

1 Like

thanks

The 256 eight bit constants B00000000 through B11111111 are predefined for you by the Arduino IDE.

Also, all shorter (7-bit, 6-bit... 1-bit) binary numbers. That's why you can't use a variable name of B0, B1, B00, B01, B10, B11...

Note: Just like the hexadecimal prefix: 0x00/0X00, the 'b' is not case sensitive so you can use 0B1110111111111111 as well as 0b1110111111111111.

One of the most useless "helpful" additions of the "Arduino Language". What the heck is wrong with the standard 0bxxxxxxxx format?

It's almost as dumb as:

typedef bool boolean;
1 Like

**Just FYI - **
Most programmers will use hexadecimal instead of binary. It's actually easier and less error-prone (especially with 16-bits or more) because you don't have to count the bit-positions.

You can easily learn to convert between hex & binary (of any size) in your head. It's a LOT easier than converting between binary & decimal... Every nybble (4-bit "pattern") converts exactly to a hex value so you only have to learn 16 conversions and you already know 0000 & 0001 and you may already know F hex = 1111 binary.

It depends on what the data represents. A bitmap image, for example, can usefully be written in binary—a 1 is black and a 0 is white (or vice versa)—and it's fairly easy to see the image right in the code.

Then waht is the difference between the following two styles of declarations/definitions?

byte x = 0b10101010;
byte x = B10101010;

The first one works on many compilers. The second one only works in an Arduino sketch (and for .cpp files, only if Arduino.h is included).

1 Like

Perhaps ANY complier that conforms to the C++ standard?

For C++14 and above.
https://en.cppreference.com/w/cpp/language/integer_literal

For earlier standards it's not included. I'm not sure what C++ standard the Arduino IDE is using.

I was reading pins and bitshifting and all that good stuff. Thats why I used binary formating

B -- can have no more than 8 bits.
0b -- can have 'any' number of bits

1 Like

It wasn't "standard."

JohnWasser says it is standard starting in 2014. Arduino was written in ~2003. I'm not sure that even gcc had binary constants in 2003.

(Hmm. The oldest version I have lying around is 0013, and that's using a compiler from 2008. It does have the binary constant support.)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.