Is it possible to do memcmp by bits instead of bytes?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Is it possible to do memcmp by bits instead of bytes?

1,715 Views
utsavikalpesh
Contributor V

Hi

I have created a project in which I am comparing bytes by memcmp().

But problem is memcmp compares only bytes.

Code I have written is :

StateChange = memcmp(&CurrentState, &PreviousStates, sizeof(CurrentState));

Here I have compare this two variables,but I want to compare individual bits.

So can anybody tell me how to compare bits by using memcmp () or Is there any other way (except xor) then plz let me know.

I am using MQX v4.0 and codewarrior 10.2.

Plz Help

0 Kudos
Reply
3 Replies

1,024 Views
utsavikalpesh
Contributor V

Hi Sol and Matthew,

Thnx for providing me information.

0 Kudos
Reply

1,024 Views
matthewkendall
Contributor V

This is a C question rather than anything specific to MXQ or Codewarrior. The answer depends on the type of your variables.

If CurrentState and PreviousStates are native types (unsigned int, etc.) then you check for equality with ==. If they are ints representing bitfields then you can check for individual bit changes with exclusive-or. You say you don't want to do that but it is not clear why; it is the correct method.

If the variables are structs then you can use memcmp() but it is dangerous. The problem is that if your structs have holes due to alignment, the holes will be filled with data that can be ill-defined, so memcmp() can say that the areas of memory differ even though every element of each struct is the same. In this case you should check all the elements of the structs for equality individually.

1,024 Views
soledad
NXP Employee
NXP Employee

Hi,

You need to create your own algorithm in order to compare individual bits.

Please check the following link for some examples.

http://stackoverflow.com/questions/7465252/bitwise-not-in-byte-compare


Have a great day,
Sol

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------