Trent Stewart
Trent Stewart

Reputation: 881

C# Bitwise With Integers

I am trying to preform a simple bitwise statement to see if a user has security. It seems to be ok until i introduce variables.

This Works: byte test = 1 & 3.

Won't work: byte a = 1; byte b = 3; byte test = a & b;

Is there anyway that I can get this to work?

Upvotes: 1

Views: 173

Answers (1)

Karl Anderson
Karl Anderson

Reputation: 34846

You need to cast it back to a byte as a bitwise AND will return an int, so do this:

byte a = 1;
byte b = 3;
byte test = (byte)(a & b);

Upvotes: 4

Related Questions