Results 1 to 2 of 2

Thread: Help with bitwise &

  1. #1

    Thread Starter
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Question 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."

  2. #2
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    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
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width