Search code examples
cbit-manipulationbitwise-operators

How to check redundant value using Bitwise AND and bitwise NOT Operator?


I have got a below snippet of code to check the RAM redundant value using bitwise Operator

uint16 initial_value = (uint16)(myvalue_32bit & 0xFFFFu); 
uint16 toggle_value = ~((uint16)(myvalue_32bit >>16));

if(initial_value == toggle_value)
{
  print("correct value");
}
else
{
  print("false value");
}

I didn't understand what is happening in the first two lines of the code. Can somebody explain why the bitwise AND and bitwise NOT operator using here ? May be explanation with one example is helpful.


Solution

  • Your 32 bit value is constructed from two parts.

    1. Lower 16 bits part.
    2. Higher 16 bits parts having the value of the binary negated lower part
    • The toggle_value is taking the higher part as new 16 bits unsigned value, then binary negates it. As per p.2 it should has value of negated lower part, now it should have the same value as the lower part of your 32 bit value.
    • initial_value is holding the lower part of the 32 bit value.

    If they are equal it means that the the 32 bit value is correct (in understanding of this error check algorithm).