473,387 Members | 1,463 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Bit masking a byte with an enum

I have an enum that is derived from byte and a member that is of type byte.
The compiler complains when I try to OR the one of the enum values with the
byte. Why?

[Flags]
public enum eDataPosition : byte
{
ePosition1 = 0x08, //00 001 000
ePosition2 = 0x10, //00 010 000
ePosition3 = 0x18, //00 011 000
ePosition4 = 0x20, //00 100 000
ePosition5 = 0x28, //00 101 000
MASK = 0x38 //00 111 000
};

public byte m_BitMask = 0;

public eDataPosition DataPosition
{
get { return (eDataPosition)(m_PacketMask | eDataPosition.MASK); }
set { m_PacketMask |= value; }
}
I get the following errors:

Operator '|' cannot be applied to operands of type 'byte' and
'TestInterface.Test.eDataPosition'
Cannot implicitly convert type 'TestInterface.Test.eDataPosition' to 'byte'
Operator '|=' cannot be applied to operands of type 'byte' and
'TestInterface.Test.eDataPosition'
I know I can cast them back and forth but why do I have to?
I tried it with and without the "Flags" attribute and with and without the
byte base-type. I get the same errors either way.

Thanks,
jim
Nov 16 '05 #1
6 12140
Hi Jim,

There is no implicit conversion between enum objects and their base type.
Flags attribute doesn't help either it is only used by ToString method. If
you need byte flages my suggestion is to use byte constants or declare
m_BitMask to be of type eDataPosition

eDataPosition m_BitMask = (eDataPosition) XXX;

in the case of zero you can write
eDataPosition m_BitMask = 0;

--
HTH
Stoitcho Goutsev (100) [C# MVP]
"Jim H" <no****@jimsaccount.com> wrote in message
news:Ol**************@TK2MSFTNGP09.phx.gbl...
I have an enum that is derived from byte and a member that is of type byte. The compiler complains when I try to OR the one of the enum values with the byte. Why?

[Flags]
public enum eDataPosition : byte
{
ePosition1 = 0x08, //00 001 000
ePosition2 = 0x10, //00 010 000
ePosition3 = 0x18, //00 011 000
ePosition4 = 0x20, //00 100 000
ePosition5 = 0x28, //00 101 000
MASK = 0x38 //00 111 000
};

public byte m_BitMask = 0;

public eDataPosition DataPosition
{
get { return (eDataPosition)(m_PacketMask | eDataPosition.MASK); }
set { m_PacketMask |= value; }
}
I get the following errors:

Operator '|' cannot be applied to operands of type 'byte' and
'TestInterface.Test.eDataPosition'
Cannot implicitly convert type 'TestInterface.Test.eDataPosition' to 'byte' Operator '|=' cannot be applied to operands of type 'byte' and
'TestInterface.Test.eDataPosition'
I know I can cast them back and forth but why do I have to?
I tried it with and without the "Flags" attribute and with and without the
byte base-type. I get the same errors either way.

Thanks,
jim

Nov 16 '05 #2
Hi Jim,

There is no implicit conversion between enum objects and their base type.
Flags attribute doesn't help either it is only used by ToString method. If
you need byte flages my suggestion is to use byte constants or declare
m_BitMask to be of type eDataPosition

eDataPosition m_BitMask = (eDataPosition) XXX;

in the case of zero you can write
eDataPosition m_BitMask = 0;

--
HTH
Stoitcho Goutsev (100) [C# MVP]
"Jim H" <no****@jimsaccount.com> wrote in message
news:Ol**************@TK2MSFTNGP09.phx.gbl...
I have an enum that is derived from byte and a member that is of type byte. The compiler complains when I try to OR the one of the enum values with the byte. Why?

[Flags]
public enum eDataPosition : byte
{
ePosition1 = 0x08, //00 001 000
ePosition2 = 0x10, //00 010 000
ePosition3 = 0x18, //00 011 000
ePosition4 = 0x20, //00 100 000
ePosition5 = 0x28, //00 101 000
MASK = 0x38 //00 111 000
};

public byte m_BitMask = 0;

public eDataPosition DataPosition
{
get { return (eDataPosition)(m_PacketMask | eDataPosition.MASK); }
set { m_PacketMask |= value; }
}
I get the following errors:

Operator '|' cannot be applied to operands of type 'byte' and
'TestInterface.Test.eDataPosition'
Cannot implicitly convert type 'TestInterface.Test.eDataPosition' to 'byte' Operator '|=' cannot be applied to operands of type 'byte' and
'TestInterface.Test.eDataPosition'
I know I can cast them back and forth but why do I have to?
I tried it with and without the "Flags" attribute and with and without the
byte base-type. I get the same errors either way.

Thanks,
jim

Nov 16 '05 #3
nal
Well..eDataPosition is a distinct enum value type, The current C# spec says
an explicit conversion is required between an enum type and an integral
type.

So even if the eDataPosition's integral type is byte, you still have to use
an explicit cast. Clear as mud? :)


"Jim H" <no****@jimsaccount.com> wrote in message
news:Ol**************@TK2MSFTNGP09.phx.gbl...
I have an enum that is derived from byte and a member that is of type byte. The compiler complains when I try to OR the one of the enum values with the byte. Why?

[Flags]
public enum eDataPosition : byte
{
ePosition1 = 0x08, //00 001 000
ePosition2 = 0x10, //00 010 000
ePosition3 = 0x18, //00 011 000
ePosition4 = 0x20, //00 100 000
ePosition5 = 0x28, //00 101 000
MASK = 0x38 //00 111 000
};

public byte m_BitMask = 0;

public eDataPosition DataPosition
{
get { return (eDataPosition)(m_PacketMask | eDataPosition.MASK); }
set { m_PacketMask |= value; }
}
I get the following errors:

Operator '|' cannot be applied to operands of type 'byte' and
'TestInterface.Test.eDataPosition'
Cannot implicitly convert type 'TestInterface.Test.eDataPosition' to 'byte' Operator '|=' cannot be applied to operands of type 'byte' and
'TestInterface.Test.eDataPosition'
I know I can cast them back and forth but why do I have to?
I tried it with and without the "Flags" attribute and with and without the
byte base-type. I get the same errors either way.

Thanks,
jim

Nov 16 '05 #4
nal
Well..eDataPosition is a distinct enum value type, The current C# spec says
an explicit conversion is required between an enum type and an integral
type.

So even if the eDataPosition's integral type is byte, you still have to use
an explicit cast. Clear as mud? :)


"Jim H" <no****@jimsaccount.com> wrote in message
news:Ol**************@TK2MSFTNGP09.phx.gbl...
I have an enum that is derived from byte and a member that is of type byte. The compiler complains when I try to OR the one of the enum values with the byte. Why?

[Flags]
public enum eDataPosition : byte
{
ePosition1 = 0x08, //00 001 000
ePosition2 = 0x10, //00 010 000
ePosition3 = 0x18, //00 011 000
ePosition4 = 0x20, //00 100 000
ePosition5 = 0x28, //00 101 000
MASK = 0x38 //00 111 000
};

public byte m_BitMask = 0;

public eDataPosition DataPosition
{
get { return (eDataPosition)(m_PacketMask | eDataPosition.MASK); }
set { m_PacketMask |= value; }
}
I get the following errors:

Operator '|' cannot be applied to operands of type 'byte' and
'TestInterface.Test.eDataPosition'
Cannot implicitly convert type 'TestInterface.Test.eDataPosition' to 'byte' Operator '|=' cannot be applied to operands of type 'byte' and
'TestInterface.Test.eDataPosition'
I know I can cast them back and forth but why do I have to?
I tried it with and without the "Flags" attribute and with and without the
byte base-type. I get the same errors either way.

Thanks,
jim

Nov 16 '05 #5
Thanks to both of you. That explains it. I thought the enum was deriving
from type byte so byte would be its base. I now realize that byte is just
it's integral type and not base.

Thanks again for the quick responses.

jim
Nov 16 '05 #6
Thanks to both of you. That explains it. I thought the enum was deriving
from type byte so byte would be its base. I now realize that byte is just
it's integral type and not base.

Thanks again for the quick responses.

jim
Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: The Last Gunslinger | last post by:
We have an enum: public enum EnumDays { Sun = 1, Mon = 2, ... } We can store it as a byte:
1
by: todorov-fkt | last post by:
Hello everyone, I have a field which is 1 byte long and is used to store different flags (according to specs) - it is the flags bit in the ID3 tag header. So 0xabc00000 represents the byte,...
1
by: J M | last post by:
I have program that query printer status from remote printer server in byte format. Some part of byte is in form of enum (integer). How do I convert to integer list or array or enum? I just need...
5
by: Gianmaria I. | last post by:
Hi, having a BitArray, how can i extract bits to create a System.byte as in the example... With BitArray bits and Byte myNewByte
25
by: Charles Law | last post by:
I thought this was going to be straight forward, given the wealth of conversion functions in .NET, but it is proving more convoluted than imagined. Given the following <code> Dim ba(1) As...
7
by: StupidScript | last post by:
>From the manual "Storage Requirements": "ENUM('value1','value2',...) =1 or 2 bytes, depending on the number of enumeration values (65,535 values maximum)" This seems to mean: "a" = 1 byte...
5
by: jeremyje | last post by:
I'm writing some code that will convert a regular string to a byte for compression and then beable to convert that compressed string back into original form. Conceptually I have.... For...
9
by: feck | last post by:
Hi, Got a query, don't know if anyone would be kind enough to help... I have to either match a string exactly eg "123456789ABCDEF123456789ABCDEF" == "123456789ABCDEF123456789ABCDEF", or if a...
9
by: Daniel Smedegaard Buus | last post by:
Hey all :) I was wondering about the $error_types (I particularly notice the 's' suffix when reading the manual) parameter for 'set_error_handler()': Can be used to mask the triggering of the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.