English 中文(简体)
交换整型变量而不使用任何赋值
原标题:Swap Integer variables without using any assignment

all! Could someone please help me to solve such a task. I need to swap variable values without using any assignment signs. I tried to do it with a while loop but I coudn t store counter value anywhere. Thank you all in advance.

最佳回答

您可以使用已互锁。Exchange

问题回答

Since integers are immutable in C#, you cannot "Swap Integer variables without using any assignment"; you will need to reassign them somewhere, somehow. Perhaps you mean Swap two variables without using a temp variable? Or, if you meant without explictly using =, @Konrad Rudolph s answer is the way to go.

Xor交换使用赋值。但是,也许您可以使用递增和递减(严格地说,在C#中,这两种方法解析为+=1-=1

int tmp = 0; // C# complains if we don’t initialize!
while (a-- > 0)
    tmp++;
while (b-- > 0)
    a++;
while (tmp-- > 0)
    b++;

这是一种有时用于分析原始演算的逻辑,例如LOOP程序形式。当然,这些形式主义不需要初始化字段,否则整个“不赋值”规则将完全没有意义。在严格的演算中,tmp也必须由循环初始化为零:

while (tmp > 0)
   tmp--;

无论tmp之前有什么值,这都会起作用(提供mp>;0,这通常是所有这些演算中的一个要求:负数不存在)。

但是,再次强调这一点,C#<em>要求

正如@Doc Brown在评论中指出的那样,这只适用于(正!)整数——尽管从理论上讲(再一次:不适用于C#!),这可以用于冯-诺依曼体系结构上可以表示的任何类型,因为它们都以数字的形式存在于内存中(以一定的基数)。

class Program
{
    private static void Swap(ref int a, ref int b)
    {
        int.TryParse((a ^ b).ToString(), out a);
        int.TryParse((a ^ b).ToString(), out b);
        int.TryParse((a ^ b).ToString(), out a);
    }

    static void Main(string[] args)
    {
        int a = 42;
        int b = 123;
        Console.WriteLine("a:{0}
b:{1}", a, b);
        Swap(ref a, ref b);
        Console.WriteLine("a:{0}
b:{1}", a, b);
    }
}

请参阅Bit Twiddling Hacks它将向您展示如何在不使用赋值的情况下以多种不同的方式进行操作。

xor交换计数吗?

     x ^= y;
     y ^= x;
     x ^= y;
int i = 10, j = 20;
i = i + j;
j = i - j;
i = i - j;

我认为这在C#中是不可能的。

XOR答案是标准的,但您只能在较低级别的语言中获取内存并直接操作其值。

.net语言将允许您执行相当于XOR运算的操作并返回值,但据我所知,要存储它,您仍然需要分配它。据我所知,你只是没有直接的内存访问权限来以这种方式执行操作。。。





相关问题
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. ...