Go Down

### Topic: Can the B(it) Type Be Expanded for Integer and Long? (Read 1 time)previous topic - next topic

#### BrendaEM

##### 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

#1
##### 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.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### pert

#2
##### 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
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.

#### AWOL

#3
##### Apr 26, 2017, 10:38 am
It's more portable anyway.
Or do what grown-ups do, and learn hex.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#### robtillaart

#4
##### 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);

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### Delta_G

#5
##### 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.
If at first you don't succeed, up - home - sudo - enter.

#### westfw

#6
##### May 05, 2017, 12:23 pm
The "0b" syntax is not part of standard C or C++.  (but it's pretty commonly implmented!)

#### oqibidipo

#7
##### 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.

Go Up