-
Help with bitwise &
I've just abou finished translating this code to VB.Net. One of the things that stood out to me was this (in bold):
Code:
uint cs = 0;
for (int i = 0; i < pingCommand.Length; i = i+2)
cs += BitConverter.ToUInt16(pingCommand, i);
cs = ~((cs & 0xffffu) + (cs >> 16));
If it's 0xffffu, thats 65535 in decimal or all 1's in binary. If you use the the AND operator, won't it always be cs no matter what cs is?
The only reason I'm asking is I'm having a rough time translating the bolded snippet. I was wondering if I could replace it with just "cs."
-
Re: Help with bitwise &
You're right, but if CS is a negative number, then you get odd results using an AND operation on it. I'm not sure if that's the purpose of that code or not. Also, if you have an integer value greater than 65535, it returns a value smaller than or equal to 65535.
Bill