什么是不好移32位变量32位? [英] What's bad about shifting a 32-bit variable 32 bits?

查看:88
本文介绍了什么是不好移32位变量32位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我拿起应用密码学的副本由布鲁斯和它是一个很好看的。我现在明白书中的工作概述了几种算法,我想开始用C实现他们几个。

I recently picked up a copy of Applied Cryptography by Bruce Schneier and it's been a good read. I now understand how several algorithms outlined in the book work, and I'd like to start implementing a few of them in C.

有一件事情,很多算法的共同点是划分一个x位密钥,分成几个小的Y型键位。例如,河豚的关键,X,是64位,但你需要它分解成两个32位半; XL和XR。

One thing that many of the algorithms have in common is dividing an x-bit key, into several smaller y-bit keys. For example, Blowfish's key, X, is 64-bits, but you are required to break it up into two 32-bit halves; Xl and Xr.

这是在那里我被卡住。我用C还算过得去,但我不是最强的,当谈到位运算符等等。

This is where I'm getting stuck. I'm fairly decent with C, but I'm not the strongest when it comes to bitwise operators and the like.

在IRC上的一些帮助,我设法想出这两个宏:

After some help on IRC, I managed to come up with these two macros:

#define splitup(a, b, c) {b = a >> 32; c = a & 0xffffffff; }
#define combine(a, b, c) {a = (c << 32) | a;}

其中,A是64位,b和c是32位​​。但是,编译器警告我有关我被32位移位32位变量的事实。

Where a is 64 bits and b and c are 32 bits. However, the compiler warns me about the fact that I'm shifting a 32 bit variable by 32 bits.

我的问题是:


  • 什么是不好移32位变量32位?我猜这是不确定的,但这些宏似乎是工作。

  • 此外,你会建议我去这个另一种方式?

正如我所说的,我相当熟悉C,但位运算符之类的还让我头痛。

As I said, I'm fairly familiar with C, but bitwise operators and the like still give me a headache.

修改

我想通了,我结合宏观实际上并没有将两个32位的变量,而只是由一个或运算0,并得到​​一个结果。结果
所以,对我的previous问题前,我还没有结合两个32位变量的方法来获得一个64位之一;如何做到这将是AP preciated建议。

I figured out that my combine macro wasn't actually combining two 32-bit variables, but simply ORing 0 by a, and getting a as a result.
So, on top of my previous questions, I still don't have a method of combining the two 32-bit variables to get a 64-bit one; a suggestion on how to do it would be appreciated.

推荐答案

是的,这是不确定的行为。

Yes, it is undefined behaviour.

ISO / IEC 9899:1999 6.5.7 按位移位运算符¶3

ISO/IEC 9899:1999 6.5.7 Bitwise shift operators ¶3

整数促销活动在每个操作数执行。该结果的类型是,促进左操作数。如果右操作数的值是负的或大于或等于促进左操作数的宽度,其行为是不确定的。

The integer promotions are performed on each of the operands. The type of the result is that of the promoted left operand. If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.

C11即ISO / IEC 9899:2011说了同样的

您应该先投 B 目标整数类型。另一点是,你应该把周围的宏参数的括号,以避免运营商precedences惊喜。此外,逗号操作符是非常有用的在这里,可以让你避免了括号,这样的宏可以作为一个普通的命令,关闭一个分号。

You should first cast b to the target integer type. Another point is that you should put parentheses around the macro parameters to avoid surprises by operator precedences. Additionally, the comma operator is very useful here, allowing you to avoid the braces, so that the macro can be used as a normal command, closed with a semicolon.

#define splitup(a,b,c) ( (b) = (a) >> 32, (c) = (a) & 0xffffffff )
#define combine(a,b,c) ( (a) = ((unsigned long long)(b) << 32) | (c) )

其他的演员可能是必要的`splitup由过度偏执的编译器保持沉默precision损失的警告。

Additional casts may be necessary for `splitup to silence warnings about precision loss by over-paranoid compilers.

#define splitup(a,b,c) ( (b) = (unsigned long)((a) >> 32), (c) = (unsigned long)((a) & 0xffffffff) )

和千万别想用你自己编写的加密生产code。

And please don't even think about using your self-written encryption for production code.

这篇关于什么是不好移32位变量32位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆