Bug in bitWrite ?

I have a function that is supposed to set or clear bits in a global variable

byte outData[4];

void WriteData( int bitNo, int val )
{
int bytePos = bitNo / 8;
int bitPos = bitNo % 8;
/*
// this commented out bit works perfectly
if ( val == 0 )
bitClear( outData[ bytePos ], bitPos);
else
bitSet( outData[ bytePos ], bitPos);
*/

// but this bitWrite function will clear bits but not set them
bitWrite( outData[ bytePos ], bitPos, (val != 0) ? (1) : (0) );
delay(2000);
WritePort(bytePos,outData[ bytePos ]);
}

Looks like a bug to me - unless I am missing something somewhere....

Dave.

#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))

So

bitWrite( outData[ bytePos ], bitPos, (val != 0) ? (1) : (0) );

expands to:

(val != 0) ? (1) : (0) ? bitClear(outData[ bytePos ], bitPos) : bitSet (outData[ bytePos ], bitPos);

Try instead:bitWrite( outData[ bytePos ], bitPos, val);

bitWrite( outData[ bytePos ], bitPos, (val != 0) ? (1) : (0) );

You want to do this rather.

bitWrite( outData[ bytePos ], bitPos, val);

or

bitWrite( outData[ bytePos ], bitPos, ((val != 0) ? (1) : (0)) );

Note the braces.

You must mean parentheses instead of brackets.

You must mean parentheses

I do ::slight_smile: