English 中文(简体)
C#中最短的方式是使用双向否定操作者写一种固定的“逐”类型?
原标题:Shortest way in C# to write a constant of type "byte" using the bitwise negation operator?
  • 时间:2010-08-26 19:49:38
  •  标签:
  • c#

这是“软问题”的范畴,但基本上,Im试图采取这种模式,利用隐性转换成特:

byte x = 0;
x |= 2;       // implicit conversion of "x | 2" to byte here

http://www.em>removingbits:

x &= ~2;      // compile error

我可以谈得最短的是:

x &= unchecked((byte) ~2);

(在这一点上,我开始认真考虑仅写以下文字:<代码>x &=253;/ ~2<>/code>,改写,或仅考虑精老的明文:x = (byte)(x &~2);

我没有一个较短的路?

最佳回答

如何做到这一点:

{
   byte x = 0;
   x |= 2;
   x &= 0xFF & ~2;
}

解释: 这里有两个问题。 第一,固定运营商:

C# Spec 4.1.5:

The integral-type unary and binary operators always operate with signed 32-bit precision, unsigned 32-bit precision, signed 64-bit precision, or unsigned 64-bit precision:

For the unary + and ~ operators, the operand is converted to type T, where T is the first of int, uint, long, and ulong that can fully represent all possible values of the operand. The operation is then performed using the precision of type T, and the type of the result is T.

一旦您申请独立经营人,结果总是在最小的某个类型。 从那以后,你想要暗中地转换成秘密。

第二,

A constant expression of type int can be converted to sbyte, byte, short, ushort, uint, or ulong, provided the value of the constant expression is within the range of the destination type.

因此,大约2,总是暗中。 不能不言而喻地加以转换,因为它不属于范围。 如果你限制在幅度之内,你可以暗中转换。

问题回答

你们可以这样说:

[Flags]
enum SomeType : byte {
  Value = 2,
}

public void Method () {
  SomeType t = 0;
  t |= SomeType.Value;
  t &= ~SomeType.Value;
}

使用习俗类型

    public struct Bits
    {
        byte x;
        public Bits(int x) { this.x = (byte)x; }
        public Bits(byte x) { this.x = x; }            
        public Bits(Bits x) { this.x = x.x; }

        public static implicit operator byte(Bits x) { return x.x; }
        public static implicit operator Bits(byte x) { return new Bits(x); }
        public static implicit operator Bits(int x) { return new Bits(x); }
    }

    static void Main(string[] args)
    {
        Bits x = 0;
        x |= 2;
        x &= ~2;
    }

此外,你还可以用逐级推展法确定借方。

    public static byte Set(this byte x, byte v)
    {
        return (byte)(x | v);
    }
    public static byte Unset(this byte x, byte v)
    {
        return (byte)(x & ~v);
    }
    static void Main(string[] args)
    {
        byte x = 0;
        x = x.Set(2);
        x = x.Unset(2);
    }
byte x = 6;
Console.WriteLine(x & ~(byte)2);

我的著作,第4版





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签