I’m working on a C programming assignment, I need to simulate the operation of a 3 bit decoder. My compiler is complaining, this Wikipedia article gives a list of C operators, but my code still seems not to be working.
Wikipedia: http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Bitwise_operators
Compiler error:
logic.c: In function ‘three_bit_decoder’:
logic.c:33: warning: statement with no effect
logic.c:33: error: expected ‘;’ before ‘~’ token
logic.c:34: warning: statement with no effect
logic.c:34: error: expected ‘;’ before ‘b0’
logic.c:35: warning: statement with no effect
logic.c:35: error: expected ‘;’ before ‘~’ token
logic.c:36: warning: statement with no effect
logic.c:36: error: expected ‘;’ before ‘b0’
logic.c:38: warning: statement with no effect
logic.c:38: error: expected ‘;’ before ‘b0’
logic.c:39: warning: statement with no effect
logic.c:39: error: expected ‘;’ before ‘b0’
logic.c:40: warning: statement with no effect
logic.c:40: error: expected ‘;’ before ‘~’ token
logic.c:41: warning: statement with no effect
logic.c:41: error: expected ‘;’ before ‘b0’
logic.c:43: warning: return makes integer from pointer without a cast
logic.c:43: warning: function returns address of local variable
make: *** [logic.o] Error 1
Code:
int three_bit_decoder(BIT b0, BIT b1, BIT b2) {
/* Returns an 8 bit number, by 2^3
*
*/
int myIntDecoder[8];
myIntDecoder[0] ~b0 + ~b1 + ~b2;
myIntDecoder[1] b0 + ~b1 + ~b2;
myIntDecoder[2] ~b0 + b1 + ~b2;
myIntDecoder[3] b0 + b1 + ~b2;
myIntDecoder[4] b0 + ~b1 + ~b2;
myIntDecoder[5] b0 + ~b1 + b2;
myIntDecoder[6] ~b0 + b1 + b2;
myIntDecoder[7] b0 + b1 + b2;
return myIntDecoder;
}
If those are assignment statements you’re missing equal signs.
The next problem you’ll encounter is that
int myIntDecoder[8]declares an array of 8 ints, which is not the same as an 8-bit int. Acharis 8 bits wide on (almost) all platforms; or you can be more explicit and use one of the standard typedefs:So as not to leave you hanging, I should mention that assigning to individual bits in a variable is not as simple as
byte[5] = 1. Doing it requires some clever use of shifting and other bitwise operations.