Go Down

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

BrendaEM

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

CrossRoads

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

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

Use "0bxxxxx" instead.
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.

robtillaart


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

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

The "0b" syntax is not part of standard C or C++.  (but it's pretty commonly implmented!)

Go Up