Bit wise operations in C
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.
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;
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. A char
is 8 bits wide on (almost) all platforms; or you can be more explicit and use one of the standard typedefs:
#include <stdint.h>
uint8_t myIntDecoder;
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.
The bitwise operators are not your problem, you have syntax errors in your code. Think about how to do assignment of variables and the solution should come to you.
Presumably you're after something like:
myIntDecoder[0] = ~b0 + ~b1 + ~b2;
// and likewise for the other 7 lines
That won't help with trying to return myIntDecoder
though. For that, you'd probably want to move the array outside the function and (for one example) pass its address into the function.
you are missing the equals operator.
myIntDecoder[7] = b0 + b1 + b2;
Also the not operator in c is !
not ~
精彩评论