Go Down

Topic: Bug in bitWrite ? (Read 925 times) previous topic - next topic

Calistra

Jun 25, 2010, 11:22 am Last Edit: Jun 25, 2010, 11:25 am by calistra Reason: 1
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.

Groove

#1
Jun 25, 2010, 11:44 am Last Edit: Jun 25, 2010, 11:47 am by GrooveFlotilla Reason: 1
Code: [Select]
#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
So
Code: [Select]
bitWrite( outData[ bytePos ], bitPos, (val != 0) ? (1) : (0) );


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

Try instead:
Code: [Select]
bitWrite( outData[ bytePos ], bitPos, val);
Per Arduino ad Astra

leppie

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


You want to do this rather.

Code: [Select]
bitWrite( outData[ bytePos ], bitPos, val);

or

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

Note the braces.

bperrybap

You must mean parentheses instead of brackets.

leppie

Quote
You must mean parentheses


I do   ::)

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy