CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

NOT Operator

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
aaronik19



Joined: 25 Apr 2011
Posts: 297

View user's profile Send private message

NOT Operator
PostPosted: Wed Dec 21, 2011 7:26 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Dec 21, 2011 7:43 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Dec 21, 2011 7:44 am     Reply with quote

so how I can achieve my code please?
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Wed Dec 21, 2011 7:52 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Dec 21, 2011 7:53 am     Reply with quote

variable2 = (!variable1);

Which separates the inversion from the assignment.

Best Wishes
Ttelmah



Joined: 11 Mar 2010
Posts: 19505

View user's profile Send private message

PostPosted: Wed Dec 21, 2011 8:12 am     Reply with quote

Yes, as RF_developer says from what you show you want the bitwise not operator, 'not' the logical not anyway!.... Smile

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

View user's profile Send private message

PostPosted: Sun Dec 25, 2011 2:23 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group