Search code examples
c++bit-manipulationpseudocode

Ambiguous pseudocode phrase


I was looking though some pseudocode for some code and I came across the phrase

lowest 32 bits of

After a very long time of searching through website I came across the answer:

What we want to do is AND operation on 64 bit number and 0xffffffff to get lowest 32 bits

My code

MT[i] = ((1812433253*(MT[i-1]^(((MT[i-1]))>>30)))+i) && 0xffffffff;

If answer is correct, can you give me a quick explanation of why this answer is correct but if this answer is not correct can you give me the correct answer.

Will this code give me the lowest 32 bits of ((1812433253*(MT[i-1]^(((MT[i-1]))>>30)))+i) ?

Answer Website


Solution

  • You are using logical AND:

    MT[i] = ((1812433253*(MT[i-1]^(((MT[i-1]))>>30)))+i) && 0xffffffff;
    

    You want to use bitwise AND:

    MT[i] = ((1812433253*(MT[i-1]^(((MT[i-1]))>>30)))+i) & 0xffffffff;
    

    The first expression will give you either true (if the first value is nonzero) or false. The second expression will give you the lowest 32 bits of that value.