Bitwise operator >> and &

I'm trying to understand a bit of code here:

1
2
3
4
5
6
7
8
9
10
11
12
13
(...)
#define MAX_UNSIGNED_SHORT                  (0xffff)

#define RANDOM_A      (1103515245)
#define RANDOM_C      (12345)
#define RANDOM_SHIFT  (16)

m_ulRandomSeed = ((RANDOM_A * m_ulRandomSeed) + RANDOM_C);

unsigned short us = ((unsigned short)((((m_ulRandomSeed >> RANDOM_SHIFT) & MAX_UNSIGNED_SHORT) * ((unsigned long)usNum)) / (MAX_UNSIGNED_SHORT + 1)));

return us;
}

I think I understand that m_ulRandomSeed >> RANDOM_SHIFT is the equivalent of dividing m_ulRandomSeed by 2^16. Is this right? I'm also wondering about the purpose of "comparing" m_ulRandomSeed with MAX_UNSIGNED_SHORT using &, and what it would result in... can you help me out? Thanks!

Maxime Bonin-Francoeur
The bitwise >> operator shifts every bit to the right and << to the left. Refer to the table http://www.cprogramming.com/tutorial/bitwise_operators.html for details of the & and | operators.

int i=1;
i=i<<1; //i=2;
i=i<<1; //=4;
i=i<<2; //=16;

i= i<<1 & 0x000000FF; //integer i can only hold a nonzero value in the (0-255) no bits will be filtered to zero in this case.
I hope this clears things up.
Thanks a lot!
Topic archived. No new replies allowed.