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

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

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

2,415件の閲覧回数
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 件の賞賛
返信
3 返答(返信)

1,724件の閲覧回数
utsavikalpesh
Contributor V

Hi Sol and Matthew,

Thnx for providing me information.

0 件の賞賛
返信

1,724件の閲覧回数
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,724件の閲覧回数
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!
-----------------------------------------------------------------------------------------------------------------------