Arduino Forum

Development => Suggestions for the Arduino Project => Topic started by: BrendaEM on Apr 26, 2017, 06:24 am

Title: Can the B(it) Type Be Expanded for Integer and Long?
Post by: BrendaEM on Apr 26, 2017, 06:24 am
The B thing B00000000 to B11111111 seems like a really handy thing to have. Wouldn't it be handy to have it for Integers? and even Longs.

Or how could a use install that in the compiling process?

Yes, I know that we are lucky to have it in the first place, as the powers that be decided not to include it in C, but wasn't C supposed to replace Assembly?

Still it would be nice to have

int I = B00000000 00000000;

I like the spaces in between.
Thanks,
BrendaEM
Title: Re: Can the B(it) Type Be Expanded for Integer and Long?
Post by: CrossRoads on Apr 26, 2017, 08:02 am
That would be really awkward to use.

However, you could find the Arduino file that has
B0 = 0;
B1 = 1;
B2 = 10;
B3 = 11;
etc.,
and come with your list for ints:
I0 = 0;
I10 = 1010;
I200 = 11001000;
etc.
Title: Re: Can the B(it) Type Be Expanded for Integer and Long?
Post by: pert on Apr 26, 2017, 08:47 am
Take a look at the file where they defined them, binary.h:
https://github.com/arduino/Arduino/blob/1.8.2/hardware/arduino/avr/cores/arduino/binary.h (https://github.com/arduino/Arduino/blob/1.8.2/hardware/arduino/avr/cores/arduino/binary.h)
It took 509 lines just for every possible value of byte. Expanding it to longs would be huge! I wonder what kind of impact that would have on compile time?

There's no magic there. You could write your own header files with all the definitions for int and long and publish them for the use of anyone else who might find them useful. Doing so manually would be incredibly boring but you could easily write a program to automatically generate the file. I can't think of a way you could make the spaces work, you could use underlines though, which is a common convention in macro names.
Title: Re: Can the B(it) Type Be Expanded for Integer and Long?
Post by: AWOL on Apr 26, 2017, 10:38 am
Use "0bxxxxx" instead.
It's more portable anyway.
Or do what grown-ups do, and learn hex.
Title: Re: Can the B(it) Type Be Expanded for Integer and Long?
Post by: robtillaart on Apr 26, 2017, 01:48 pm

All binary patterns up to 16 bits would take up 66000 lines of code, 32 bits would be a multibillion line file


you can use some simple math to do what you want

int x = B01010101 * 256 + B01010101;



put this in a macro like
    #define B16(c, d) ((c) * 256 + (d))

and you can write
    int x = B16(B01010101, B01010101) ;


or for 32 bit

#define B32(a, b,  c, d) ((((a) * 256UL + (b)) * 256UL + (c)) * 256UL + (d))

and you can write
unsigned long y = B32(B01010101, B01010101, B01010101, B01010101);

unsigned long z = B32(4, 5, 6, 7);

Title: Re: Can the B(it) Type Be Expanded for Integer and Long?
Post by: Delta_G on May 05, 2017, 07:15 am
Why not just use the 0b syntax instead of B?  It will work for any value.  I have yet to figure out why the Arduino folks felt the need to define all of those constants when the same syntax was already available and worked for all values. 
Title: Re: Can the B(it) Type Be Expanded for Integer and Long?
Post by: westfw on May 05, 2017, 12:23 pm
The "0b" syntax is not part of standard C or C++.  (but it's pretty commonly implmented!)
Title: Re: Can the B(it) Type Be Expanded for Integer and Long?
Post by: oqibidipo on Aug 21, 2017, 08:08 pm
The "0b" syntax is not part of standard C or C++.  (but it's pretty commonly implmented!)
It is part of C++14.