[英]delphi to C++ builder conversion
I have source code in Delphi I follow this http://hscripts.com/tutorials/cpp/bitwise-operators.php for bitwise operators to convert it in C++ Builder, but the result is different 我在Delphi中有源代码我按照http://hscripts.com/tutorials/cpp/bitwise-operators.php为按位运算符在C ++ Builder中转换它,但结果是不同的
Source code in Delphi Delphi中的源代码
procedure TForm1.Button1Click(Sender: TObject)
var
tmp, dynamicINT : integer;
begin
dynamicINT := 42080;
tmp := ((dynamicINT shl 1) or (dynamicINT shr 31) and $7FFFFFFF);
Edit1.Text := IntToHex(tmp, 4);
end;
Delphi result : 148C0
correct! 德尔福结果:
148C0
正确!
Source code in C++ Builder C ++ Builder中的源代码
void __fasctall TForm1::Button1Click(TObject *Sender)
{
int tmp = 0;
int dynamicINT = 42080;
tmp = ((dynamicINT << 1) || (dynamicINT >> 31) && 0x7FFFFFFF);
Edit1->Text = IntToHex(tmp, 4);
}
C++ Builder result : 0001
??? C ++ Builder结果:
0001
???
What is wrong with conversion ? 转换有什么问题?
I'm using C++ Builder 6 and Delphi 7 我正在使用C ++ Builder 6和Delphi 7
||
and &&
are logical operators in C++, not bitwise operators. 和
&&
是C ++中的逻辑运算符,而不是按位运算符。 They only return true/false. 它们只返回true / false。 The corresponding binary operators are
|
相应的二元运算符是
|
and &
. 和
&
。
Try: 尝试:
tmp = ((dynamicINT << 1) | (dynamicINT >> 31) & 0x7FFFFFFF);
You are using logical operators. 您正在使用逻辑运算符。 You need to use bitwise operators,
&
and |
您需要使用按位运算符,
&
和|
. 。 What's more the C++ code needlessly initializes and then overwrites the
tmp
variable. 更不用说C ++代码不必要地初始化然后覆盖
tmp
变量。 Your code should be: 你的代码应该是:
int dynamicINT = 42080;
int tmp = ((dynamicINT << 1) | (dynamicINT >> 31) & 0x7FFFFFFF);
One thing to be careful about in the translation is what is meant by the shift operators when applied to signed types. 在翻译中要注意的一点是,当应用于签名类型时,移位运算符的含义。 I believe that there will be no problem with the code in the question since
dynamicINT
has a fixed value. 我相信问题中的代码没有问题,因为
dynamicINT
有一个固定的值。 But perhaps in the real code it can vary. 但也许在真实的代码中它可能会有所不同。 In which case you may encounter implementation defined or undefined behaviour.
在这种情况下,您可能会遇到实现定义或未定义的行为。
I suspect that you should be using untyped variables here. 我怀疑你应该在这里使用无类型变量。 I'd have these variables as
Cardinal
in the Delphi code and unsigned int
in the C++ code. 我将这些变量作为Delphi代码中的
Cardinal
和C ++代码中的unsigned int
。
In addition to Mat's answer , it might help you for porting to know that in C/C++, as opposed to Delphi/Pascal, any bool
value is interchangeable with all integral and numeric types. 除了Mat的答案之外 ,它可能有助于您移植以了解在C / C ++中,与Delphi / Pascal相反, 任何
bool
值都可以与所有整数和数字类型互换 。 The following nonsense example will not cause any warning by the compiler: 以下废话示例不会引起编译器的任何警告:
bool a = 6 * 7.89;
int b = true & 128;
float f = a + !b; // which is 2
...and last but not least: You may use Delphi unit files unchanged in C++-Builder projects. ...最后但并非最不重要: 您可以在C ++ - Builder项目中使用未更改的Delphi单元文件。 just try to add one of them to your project (this way you can do the migration - if still needed - step by step).
只是尝试将其中一个添加到您的项目中(这样您就可以进行迁移 - 如果仍然需要 - 一步一步)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.