Tuesday, July 24, 2007

using Bit Fields

There was a question, as to write a program in C, which
when supplied 0, would print 1, and on being supplied 1
would 0. You are not to use, bit wise operators, i conditions, ternary operators. Below is my attempt at it.

#include
#include

int main(int argc, char *argv[])
{
struct packed_struct {
unsigned int f:1;
} pack;


pack.f = atoi(argv[1]);
pack.f++;

fprintf(stderr, " %d \n", pack.f);

return 0;
}

This works for me ;-)