ushort bitmask "cannot implicitly convert type 'int' to 'ushort'

I can do this:
      ushort x = 0xFFFF;
      ushort y = x;
      y &= 0x01FF;

Open in new window

but I can't do this:
      ushort y = x & 0x01FF;

Open in new window

I get the error: Cannot implicitly convert type 'int' to 'ushort'. An explicit conversion exists (are you missing a cast?)

I try throwing in casts but Visual Studio still complains:
      ushort y = (ushort)x & (ushort)0x01FF;

Open in new window

What is the proper syntax for this?
deleydAsked:
Who is Participating?
 
Raheman M. AbdulConnect With a Mentor Senior Infrastructure Support Analyst & Systems DeveloperCommented:
Try
 ushort y = (ushort) (x & 0x01FF);
0
 
russellCConnect With a Mentor Commented:
Try:
ushort z = (ushort)(x & y);   // OK: explicit conversion
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.