BitShift vs. pow() - Translating a table to MAX7219

I am trying to feed data from a table to a MAX7219 using Eberhard Fahle’s LedControl library http://playground.arduino.cc//Main/LedControl#Source. I use this table throughout the program and am using 2x MAX7219’s to display the contents of those tables.

My question is, which is better for loading table data into a variable for displaying onto the driver chip? Is there any significant functional difference between the two methods? Please keep in mind that each of the array values can only be a 1 or a zero.

for (byte x = 0; x < 8; x++) {
  byte value = 0;
  for (byte y = 0; y < 8; y++) {
    value = value + ( table[x][y] * pow(2, y) );
  }
  max7219.setRow( dev, x, value);
}

or would this be better?

for (byte x = 0; x < 8; x++) {
  byte value = 0;
  for (byte y = 0; y < 8; y++) {
    value = value + ( value << 1 );
  }
  max7219.setRow( dev, x, value);
}

Also, will feeding the library a base10 number (value) instead of a binary value (B00000001) will work?

pow() return s a floating-point value. If you store it in an integer it will get truncated.

pow(2,2) = 3.999999

Store that in an integer and you get 3.

Thanks for the help, that would have caused me headaches beyond belief. One more quick question, do you know if these two lines of code are parsed the same?

  digitalWrite(13, HIGH && (x & B00000100));
  digitalWrite(13, HIGH && (x & 4));

tzimtzum:
Thanks for the help, that would have caused me headaches beyond belief. One more quick question, do you know if these two lines of code are parsed the same?

  digitalWrite(13, HIGH && (x & B00000100));

digitalWrite(13, HIGH && (x & 4));

Inside binary.h:

#define B100 4
#define B0100 4
#define B00100 4
#define B000100 4
#define B0000100 4
#define B00000100 4

Any of the above will result in exactly the same as typing a 4.

Here is where to find that file :
.\arduino-1.0\hardware\arduino\cores\arduino

I don't know why the Arduino folks defined hundreds of binary constants, every value from 1 to 8 bits. The C++ language already has a syntax for binary constants:

0b0
0b1
0b0001
0b1111
0b00000000000100

The defines in the Arduino library cause mysterious errors if you try to name a variable B0, B1, B10, etc.

tzimtzum: Thanks for the help, that would have caused me headaches beyond belief. One more quick question, do you know if these two lines of code are parsed the same?

  digitalWrite(13, HIGH && (x & B00000100));
  digitalWrite(13, HIGH && (x & 4));

John, thanks for the insight. I didn't make variable definitions with these B01 etc. names but I can certainly avoid it now that you pointed it out. I was chasing my tails the other night when I messed up a #define, with strange lvalue error. I later realized I copy and pasted an extra '+' in the def and a later def is based on the first one.

The C++ language already has a syntax for binary constants.

This isn’t C++ standard, its a GCC extension. Which may be the reason for defining all those sets.

Here is a copy of Tom Torfs code I ported to arduino, it allows binary constants in any C++ implementation.

/* *************************************************************************************
	Binary constant generator macro 
	By Tom Torfs - donated to the public domain 

	All macro's evaluate to compile-time constants

	Sample usage: 
		B8(01010101);// = 85 
		B16(10101010,01010101);// = 43605 
		B32(10000000,11111111,10101010,01010101);// = 2164238933 
************************************************************************************* */
/* *************************************************************************************
	HEX__(n)
		Turn a numeric literal into a hex constant ( avoids problems with leading zeroes ).
		8-bit constants max value 0x11111111, always fits in unsigned long 		
************************************************************************************* */

#define HEX__(n) 0x##n##LU 

/* 8-bit conversion function */ 
#define B8__( x ) ( ( x & 0x0000000FLU ) ? 1 : 0 ) + \
			( ( x & 0x000000F0LU ) ? 2 : 0 ) + \
			( ( x & 0x00000F00LU ) ? 4 : 0 ) + \
			( ( x & 0x0000F000LU ) ? 8 : 0 ) + \
			( ( x & 0x000F0000LU ) ? 16 : 0 ) + \
			( ( x & 0x00F00000LU ) ? 32 : 0 ) + \
			( ( x & 0x0F000000LU ) ? 64 : 0 ) + \
			( ( x & 0xF0000000LU ) ? 128 : 0 )

/* for upto 8-bit binary constants */ 
#define B8( d )  ( ( uint8_t ) B8__( HEX__( d ) ) ) 

/* for upto 16-bit binary constants, MSB first */ 
#define B16( dmsb, dlsb )  ( ( ( uint16_t ) B8( dmsb ) << 8 ) + B8( dlsb ) ) 

/* for upto 32-bit binary constants, MSB first */ 
#define B32( dmsb, db2, db3, dlsb )  ( ( ( uint32_t ) B8( dmsb ) << 24 ) + ( ( uint32_t ) B8( db2 ) << 16 ) + ( ( uint32_t ) B8( db3 ) << 8 ) + B8( dlsb ) )

pYro_65:

The C++ language already has a syntax for binary constants.

This isn't C++ standard, its a GCC extension. Which may be the reason for defining all those sets.

OMG! You're right.

I've been a programmer for a LONG time and I don't remember not having a binary constant. I guess that's why I thought it was a C construct. Of course back in the early days we used HEX because it is easier to read with practice.

Of course back in the early days we used HEX because it is easier to read with practice.

And far easier to type without making mistakes. A 64 bit binary value takes a lot of care to type without making mistakes. A 64 bit binary value is far easier.

PaulS:

Of course back in the early days we used HEX because it is easier to read with practice.

And far easier to type without making mistakes. A 64 bit binary value takes a lot of care to type without making mistakes. A 64 bit binary value is far easier.

LOL, so which is it?

LOL, so which is it?

A 64 bit [u]hex[/u] value is far easier...