locked
Why do I need this cast? RRS feed

  • Question

  • When I OR two bytes together and assign it to a byte, I get an error that says cannot convert from an int to a byte, like this:

    public const byte MODE_DEBUG = 0x80;
    byte b = 0;

    b = b | MODE_DEBUG;     // give the error

    I need to do this cast:

    b = (
    byte)(b | MODE_DEBUG);


    Cannot figure out why as they are all the same type.

    Thanks,
    Rich 
    Wednesday, July 16, 2008 2:56 PM

Answers

  • because "byte operator|(byte a, byte b)" is not defined in the CLR, but "int operator|(int a, int b)" is, so the compiler silently promotes your bytes to ints.  However, it cannot silent demote the resulting int back down to a byte.


    Truth, James (http://www.honestillusion.com)
    Wednesday, July 16, 2008 5:13 PM

All replies

  • because "byte operator|(byte a, byte b)" is not defined in the CLR, but "int operator|(int a, int b)" is, so the compiler silently promotes your bytes to ints.  However, it cannot silent demote the resulting int back down to a byte.


    Truth, James (http://www.honestillusion.com)
    Wednesday, July 16, 2008 5:13 PM
  • Ha, ha, that's just plain silly.
    Wednesday, July 16, 2008 5:28 PM
  • This doesn't work either:

    byte x;
    byte y = 1;

    x = y + (byte)1;

    Come on, cannot add two bytes together and not have them be ints? Even this fails:

    byte z;
    z = x + y;

    Friday, July 18, 2008 6:38 PM
  • Rich Sienkiewicz said:

    This doesn't work either:

    byte x;
    byte y = 1;

    x = y + (byte)1;

    Come on, cannot add two bytes together and not have them be ints? Even this fails:

    byte z;
    z = x + y;


    Does work for the same reason cited by James Curran.

    There's a lot of stuff that can go on "behind the scenes" when performing arithmetic.  Search the forums for how many posts there are from folks getting the wrong values or rounding errors from decimal arithmetic.

    http://forums.msdn.microsoft.com/en-US/csharpgeneral/thread/abfbde7f-a303-4dfb-b496-3700a18c0e12
    http://forums.msdn.microsoft.com/en-US/csharpgeneral/thread/a0aac2ac-5c0a-4423-a7ba-8543f3d0f347

    Most of these error complaintss are rooted in the simple fact that repeating decimal fractions cannot be represented in binary form.  LIke the fraction 1/3, which equals  0.3333333, with an infinite number of 3s following the decimal.

    Other errors come about because of what I mentioned above, action behind the scenes.  For example.

    Decimal num1 = 25;
    Int32 num2 = 25;

    Decimal num3 = num1 + num2;

    That innocent looking code causes numerous casts.  Why?  First, 25 is an integer, so the 25 for num1 must first be cast to a decimal.  Then the sum is even worse.  Neither value being added together is of the same type.  They are both cast to a type that can represent  most every value type, float.  Then the result is cast back to a decimal.  All of this juggling can cause round-off errors.

    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, July 18, 2008 6:53 PM
    Moderator
  • Oh, totally understand all that when going to/from different datatypes and having roundoff errors and such. But jeez, I cannot add two variables of the same datatype, a byte to a byte? I can add an int to an int.
    Friday, July 18, 2008 7:03 PM
  • Rich Sienkiewicz said:

    Oh, totally understand all that when going to/from different datatypes and having roundoff errors and such. But jeez, I cannot add two variables of the same datatype, a byte to a byte? I can add an int to an int.

    I saw another post today on this identical subject.  Cannot find it at the moment.  It contained some very informative links. 

    Basically, the CPU is a 32-bit device that contains some registers.  The Framework casts everything to Int32, or at least it tries, when performing those operations.  It does not have binary operators for every ValueType.

    For most machines, 32-bits is the standard "word" size.  There is nothing stopping you from attempting to write your own code to see if it is any faster.  But, keep in mind that the Framework is optimized to run applications under Windows.

    You may wish to explore the BitVector32 type.

    Rudedog

    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, July 18, 2008 7:19 PM
    Moderator
  • James Curran said:

    because "byte operator|(byte a, byte b)" is not defined in the CLR, but "int operator|(int a, int b)" is, so the compiler silently promotes your bytes to ints.  However, it cannot silent demote the resulting int back down to a byte.


    Truth, James (http://www.honestillusion.com)


    Found that thread.

    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, July 18, 2008 8:16 PM
    Moderator