We help IT Professionals succeed at work.

C Conversion

Medium Priority
Last Modified: 2010-04-15

I need to convert the following line of code to VB.Net, can anyone explain the syntax please?

next_A1 = next_A1 ^ D ^ ((j % 2) ? in2 : in1);

Watch Question

^ is a bitwise xor operator. For example, a ^ b will toggle in 'a' any bits set in 'b'.

% is the modulus operator. a % b returns the remainder of dividing 'a' by 'b'.

? selects between two expressions. (a ? b : c) means "if a is true (nonzero), the value of the expression is b, otherwise (a is false or zero), the value of the expression is c.

I don't know VB.net, but this should get you close enough:

// pseudo basic conversion of "next_A1 = next_A1 ^ D ^ ((j % 2) ? in2 : in1);"

tmp = next_A1
    // The following evaluates "next_A1 ^ D"
tmp = result XOR D
    // The following evaluates "j % 2"
tmp2 = j MOD 2
    // The following evaluates ((j % 2) ? in2 : in1)
if tmp2 <> 0
  tmp3 = in2
  tmp3 = in1
   // The following does the final xor
result = result XOR tmp3
next_A1 = result

Here is a more compact way:

if (j MOD 2) <> 0
  next_A1 = next_A1 XOR D XOR in2
  next_A1 = next_A1 XOR D XOR in1

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts



Thank you.

My pleasure.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.