0

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

  • The double-et (`&&`) is ***logical*** and,not bitwise. Meaning the result of your expression will ***always*** be `1` or `0`. – Some programmer dude Feb 08 '15 at 00:55
  • 1
    @JoachimPileborg ... or `0`. – Barry Feb 08 '15 at 00:57
  • 1
    @Barry Of course, I thought of logical *or* which will always be `1`. Should probabl6y stop drinking now... – Some programmer dude Feb 08 '15 at 00:58
  • this was my stupid mistake but can you guys explain more thoroughly why doing an and operation between the number and 0xffffffff gives me lowest 32 bits of number. The answer website wasn't very helpful in explaining it. – Irrational Person Feb 08 '15 at 01:07
  • @IrrationalPerson It's called *masking*, and if you don't know what the bitwise operators do you really should go back to your teacher and beat him/her with a big stick! It's so fundamental to the lower levels of programming and understanding how computers actually work that everyone doing anything more than pure HTML should learn it. – Some programmer dude Feb 08 '15 at 01:09
  • I understand the basic concept of masking but why does specificly the and operation between number and 0xffffffff give me the lowest 32 bits of number. Or better asked what is the definition of lowest 32 bits? – Irrational Person Feb 08 '15 at 01:15
  • @IrrationalPerson Take a paper, and write down `0xffffffff` in binary on that paper, then count the number of ones, and then think about what doing a bitwise and with that – Some programmer dude Feb 08 '15 at 01:59
  • Why was I randomly downvoted two months after I asked this question? If you feel like there is a problem with my question please give me suggestions on how to improve this question. – Irrational Person Apr 08 '15 at 16:21

1 Answers1

2

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.

Barry
  • 286,269
  • 29
  • 621
  • 977