Solved

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

Posted on 2011-09-15
2
925 Views
Last Modified: 2012-06-27
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?
0
Comment
Question by:deleyd
2 Comments
 
LVL 19

Accepted Solution

by:
Raheman M. Abdul earned 125 total points
ID: 36542477
Try
 ushort y = (ushort) (x & 0x01FF);
0
 
LVL 3

Assisted Solution

by:russellC
russellC earned 125 total points
ID: 36542504
Try:
ushort z = (ushort)(x & y);   // OK: explicit conversion
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question