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

Invert byte / flip bits using single operation

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



Joined: 30 Oct 2007
Posts: 566
Location: Ottawa, Ontario, Canada

View user's profile Send private message

Invert byte / flip bits using single operation
PostPosted: Mon Dec 05, 2022 10:30 am     Reply with quote

Hi guys,

Not sure what I'm doing wrong here but I cannot figure-out what the problem is.... let's say I have this byte:

unsigned int16 Data_ID = 0xFF02; (1111 1111 0000 0010)

I now want to invert the bits. I thought a simple Data_ID != Data_ID; would convert the value to 0x00FD (0000 0000 1111 1101). Didn't work.

Then I tried Data_ID ~= Data_ID and that also has no effect.

In both cases, the value is always 0.

Am I missing something here?

Thanks,

Ben
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Dec 05, 2022 11:12 am     Reply with quote

Do this:
Code:
Data_ID = ~Data_ID;
benoitstjean



Joined: 30 Oct 2007
Posts: 566
Location: Ottawa, Ontario, Canada

View user's profile Send private message

PostPosted: Mon Dec 05, 2022 11:14 am     Reply with quote

Ah! I had tried it but I think I had screwed-up something else! Works now! Thanks!

Ben
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Fri Dec 09, 2022 3:16 am     Reply with quote

As a comment here, it is possibly worthwhile looking at what the operators
'do', as to why ! did not work.

! -> logical negate. Makes something that tests as true return a false, and
something that tests as false return true. So won't invert the bits, but the
logical 'result'.
~ -> bitwise not. Inverts every bit in the number. For a 16bit number, same as
^=0xFFFF
This "xor's" each bit with the corresponding bit in the second value. Since
each is 1, this does the same thing as the ~
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