|
-
January 21st, 2010, 01:40 PM
#5
Re: Can these two lines of code be simplified?
Thanks Nuzzle for the obvious one-liner that illuded me & the boolean simplification. I have learned to comment my code next time as this is my first thread- thanks ninja9578.
This code is used for clearing or setting an output for Atmel AVR microcontrollers if anyone is curious.
The function on a tutorial reads (our one-liner replaces the entire if-else statement):
int set_PORTB_bit(int position, int value)
{
// Sets or clears the bit in position 'position'
// either high or low (1 or 0) to match 'value'.
// Leaves all other bits in PORTB unchanged.
if (value == 0)
{
PORTB &= ~(1 << position); // Set bit position low
}
else
{
PORTB |= (1 << position); // Set high, leave others alone
}
return 1;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|