CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2006
    Posts
    35

    Exception on arithmetic operation

    Why does this bit of code throw an exception?

    dim dataBytes(5) as byte

    dataBytes(0)=&H4A
    dataBytes(1)=&H1
    dataBytes(2)=&H1
    dataBytes(3)=&H2A
    dataBytes(4)=(dataBytes(1)+dataBytes(2)+dataBytes(3))^&HFF

    The final statement is what generates the exception. The exception states that an arithmetic operation resulted in an overflow.

    Thanks!

  2. #2
    Join Date
    Jan 2006
    Posts
    35

    Re: Exception on arithmetic operation

    To answer my own question...

    I mixed C syntax with VB.net syntax. ^ is an XOR operation in C... in VB.net it's simply Xor. ^ is obviously valid in VB.net and so it didn't generate any errors. Dumb mistake... but I guess it's bound to happen when using both languages simultaneously.

  3. #3
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    Re: Exception on arithmetic operation

    byte type represent 8-bit unsigned integers ranges b/w 0-255. the arithematic operation cause an overflow.

    in vb.net ^ operator to raise a number to the power of an exponent.
    so use the xor operator.
    Code:
            dataBytes(4) = (dataBytes(1) + dataBytes(2) + dataBytes(3)) Xor &HFF

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

Featured