View previous topic :: View next topic |
Author |
Message |
aaronik19
Joined: 25 Apr 2011 Posts: 297
|
NOT Operator |
Posted: Wed Dec 21, 2011 7:26 am |
|
|
Dear Friend,
I do not know what is happening with the part of the program I am using. I have a variable containing a byte 0b01001011 and in a sub-function I have another variable which will hold the inverse value.
Code: |
int8 variable2;
void function1(int8 variable1)
{
variable2 != variable1; //invert and store the result
} |
I am assuming that variable2 will be 0b10110100 (inverse of 0b01001011. Am I right? For some reason it will remain 0b00000000 and the compiler is warning me with "Code has no effect".
Appreciate your help |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19505
|
|
Posted: Wed Dec 21, 2011 7:43 am |
|
|
Problem is that "!=", is the logical inequality operator. If you look at the list of operators that allow you to 'shortcut' arithmetic with assignment, you have:
+= /= %= += -- <<= >>= &= ^= |=
!, can't be used this way. So what the code is doing, is comparing variable1 with variable2, and throwing away the result (so has no effect).
Best Wishes |
|
|
aaronik19
Joined: 25 Apr 2011 Posts: 297
|
|
Posted: Wed Dec 21, 2011 7:44 am |
|
|
so how I can achieve my code please? |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Wed Dec 21, 2011 7:52 am |
|
|
In C the bitwise inversion operator is ~. As in
variable2 = ~variable1;
alternatively you can exclusive or with all ones, or any other bit pattern to invert some bits in a variable:
invert all bits in int:
variable2 = variable1 ^ 0b11111111;
invert lower four bits only:
variable2 = variable1 ^ 0b00001111;
RF Developer
Last edited by RF_Developer on Wed Dec 21, 2011 7:56 am; edited 2 times in total |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19505
|
|
Posted: Wed Dec 21, 2011 7:53 am |
|
|
variable2 = (!variable1);
Which separates the inversion from the assignment.
Best Wishes |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19505
|
|
Posted: Wed Dec 21, 2011 8:12 am |
|
|
Yes, as RF_developer says from what you show you want the bitwise not operator, 'not' the logical not anyway!....
Also, it is key to understand the difference between (for instance):
val+=1;
val= +1;
The first adds 1 to val, and is a 'shortcut' for:
val=val+1;
The second assigns the number +1 to val.
Best Wishes |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Sun Dec 25, 2011 2:23 am |
|
|
RF_Developer wrote: | In C the bitwise inversion operator is ~. |
In verilog too. _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
|