Topace
asked on
Implementing Bitwise Operators
Hello -
I am wondering if anyone knows how to implement the logical not (!) operator. I am developing a program, and it requires me to implement the logical NOT operator using only >> << + | & and ~. Yes, this is for a class, and I understand if you feel morally obliged to not give me the whole answer, but I have been working on this for quite a while now, and I am completely stumped. If someone might be able to give me a hint, that would be very much appreciated.
Thanks -
~ace
I am wondering if anyone knows how to implement the logical not (!) operator. I am developing a program, and it requires me to implement the logical NOT operator using only >> << + | & and ~. Yes, this is for a class, and I understand if you feel morally obliged to not give me the whole answer, but I have been working on this for quite a while now, and I am completely stumped. If someone might be able to give me a hint, that would be very much appreciated.
Thanks -
~ace
In C use have available the not operator (~), then what is the use of implementing it. Or, are you talking about something else. Comments ???
~ is bitwise NOT a.. while ! is logical NOT ..
the asker wants to implement logical NOT .. using the mentioned operators
the asker wants to implement logical NOT .. using the mentioned operators
you are trying to implement the logical NOT operator using the bitwise operators (<< >> + | & ~). Let me try to solve your problem. You try this simple module
bool NOT(bool x)
{
x = !x;
return(x);
}
Let me know if it works.
regards
bool NOT(bool x)
{
x = !x;
return(x);
}
Let me know if it works.
regards
Operators are implemented inside a compiler. It's part of the language. I don't think C provides facility to implement your own operators.
The only thing you can do is implement functions which mimics operators.
-Narendra
The only thing you can do is implement functions which mimics operators.
-Narendra
#define NOT(x) x > 0 ? 1 : 0
I totally agree with ssnkumar. Operators cannot be implemented. Thats' why I have provided you a function which mimics the logical NOT operator. Check it out...
I totally agree with ssnkumar. Operators cannot be implemented. Thats' why I have provided you a function which mimics the logical NOT operator. Check it out...
This is Computer Science 101.
Look in your textbook.
ASKER
See, the thing is that I am trying to implement a function that will implement the logical NOT operator. For instance, if I have anything other than zero, it produces zero, while a zero input produces 1. The deal is that I can only use a certain number of operators, and I am stuck.
~ace
~ace
Ok, in C a variable is considered true if even one of its bits is 1. It is false if all its bits are 0. So to create a NOT function we need to check every bit in the variable. Return 1 if all the bits are 0 and return 0 if even one bit is 1.
bool NOT(char x)
{
char r = 0; // will hold the result
char one = 1;
int N = 8; // the size of the variable in bits
// loop through every bit in the variable. test each
// bit and then right shift it out so that the next
// bit takes its place. OR it with r so that r
// will equal 1 even if one bit is set to 1
for(int i=0; i==N; i++) {
r = r | (x & one);
x = x>>1;
}
// now x is either 1 (if non zero) and 0 (if zero),
// therefore we need to switch it, the best way to
// do this is to xor it with 1. then 1^1 = 0 & 0^1 = 0.
// because we cant use xor we have to make it:
// A XOR B = (A & (~B)) | (B & (~A))
r = (r & (~one)) | (one & (~r));
return r; // this is now either 1 or 0 and will be
// the same as the logical !
}
Well, I think that should work and I hope it's what you're after.
Prash
bool NOT(char x)
{
char r = 0; // will hold the result
char one = 1;
int N = 8; // the size of the variable in bits
// loop through every bit in the variable. test each
// bit and then right shift it out so that the next
// bit takes its place. OR it with r so that r
// will equal 1 even if one bit is set to 1
for(int i=0; i==N; i++) {
r = r | (x & one);
x = x>>1;
}
// now x is either 1 (if non zero) and 0 (if zero),
// therefore we need to switch it, the best way to
// do this is to xor it with 1. then 1^1 = 0 & 0^1 = 0.
// because we cant use xor we have to make it:
// A XOR B = (A & (~B)) | (B & (~A))
r = (r & (~one)) | (one & (~r));
return r; // this is now either 1 or 0 and will be
// the same as the logical !
}
Well, I think that should work and I hope it's what you're after.
Prash
just realised that the function definition is wrong. It should be
char NOT(char x)
Prash
char NOT(char x)
Prash
>> I totally agree with ssnkumar. Operators cannot be implemented. Thats' why I have provided you a function which mimics the logical NOT operator.
Ditto, this can be done in C++ by overloading an operator but this CANNOT be done in C by a method other than a function.
Exceter
Ditto, this can be done in C++ by overloading an operator but this CANNOT be done in C by a method other than a function.
Exceter
He isn't trying to overload the operator, just trying to create a function...
How about this:
unsigned char not( unsigned char c ) {
return (c & 0x01) ^ 0x01;
}
anding c with 1 should result in either 0x00 or 0x01 then if you xor 0x00 with 0x01 you'll get 0x01
conversely if you xor 0x01 with 0x01 you'll get 0x00
I think it should work
How about this:
unsigned char not( unsigned char c ) {
return (c & 0x01) ^ 0x01;
}
anding c with 1 should result in either 0x00 or 0x01 then if you xor 0x00 with 0x01 you'll get 0x01
conversely if you xor 0x01 with 0x01 you'll get 0x00
I think it should work
Looks like pn206 is you answer. Could have included a test shell to varify it works :)
>>>>>Ditto, this can be done in C++ by overloading an operator but this CANNOT be done in C by a method other than a function.
macro definitions {group of statements} is for that purpose :)
macro definitions {group of statements} is for that purpose :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The negation function is very easy:-)
Just XOR the bit you want to negate with 1.
So, if you want to negate the complete 4 bytes of an integer, then XOR it with 0xffffffff. The result is NOT of that integer (the same you get when you use ~ operator with an integer).
Hope this answers your question.
Since this is very easy, I am not giving the code......I think you can do it very easily....:-)
Feedback is appreciated......
-Narendra
Just XOR the bit you want to negate with 1.
So, if you want to negate the complete 4 bytes of an integer, then XOR it with 0xffffffff. The result is NOT of that integer (the same you get when you use ~ operator with an integer).
Hope this answers your question.
Since this is very easy, I am not giving the code......I think you can do it very easily....:-)
Feedback is appreciated......
-Narendra
>> >>>>>Ditto, this can be done in C++ by overloading an operator but this CANNOT be done in C by a method other than a function.
>> macro definitions {group of statements} is for that purpose :)
Yeah, but a macro isn't an operator. :-)
Exceter
>> macro definitions {group of statements} is for that purpose :)
Yeah, but a macro isn't an operator. :-)
Exceter
ASKER
Thanks -
I just needed a hint on how to accomplish it. Just out of curiosity - how did you know I couldn't use conditionals? I take it you must have done this assignment.
It is ironic that many of you consider this basic computer science, believe it or not this is a 300 level course.
Thanks all who helped.
~ace
I just needed a hint on how to accomplish it. Just out of curiosity - how did you know I couldn't use conditionals? I take it you must have done this assignment.
It is ironic that many of you consider this basic computer science, believe it or not this is a 300 level course.
Thanks all who helped.
~ace
Knowing the answer is a good hint toward knowing the question.
And having an understanding of the hardware goes a long way toward knowing the answer. ;)
This was part of the first computer design class that I ever took (albeit 28 years ago). The necessary math was a prereq and the heart of your question goes to the math. It's kind of scary that this is now a 300 level class.
Kdo
ASKER
I agree that this is scary that it is a 300 level class - Computer Architecture. I don't agree with it - I think that basic programming should be taught before high level programming...it would make more logical sense to go in that order.
It was just a bunch of puzzles...most likely prereqs to information that comes later...but still...
~ace
It was just a bunch of puzzles...most likely prereqs to information that comes later...but still...
~ace