perl - Perl <<= and ^= Meaning
问题描述
Hello I´m trying to convert a bunch of Perl scripts to C#. A have this piece of code and I don´t know what <<=, ^=, &= means. Can someone please tell me the meaning of the part (???) too?. Thanks in advance.
$cwl = 6;
$sr = 1;
for ($i=0; $i<$gf::field; $i++) {
$gf::log[$sr] = $i;
$gf::alog[$i] = $sr;
$sr <<= 1;
if ($sr & (1<<$cwl)) {$sr ^= $prim} (???)
$sr &= $gf::field;
}
解决方案
These are:
<<=
= Left shift equal operator, shifts the source variable's bits by n places to the left and assign back to source variable.^=
= BitwiseXOR
equal operator.XOR
s the source variable by the right hand variable, and assigns to the source variable.&=
= BitwiseAND
equal operator.AND
s the source variable and the right hand variable, and assigns the result to the source variable.
The C# equivalents from your code above:
Left Shift
# Perl code
$sr <<= 1;
// C# code
sr = sr << 1;
Bitwise XOR
# Perl code
$sr ^= $prim
// C# code
sr = sr ^ prim
Bitwise AND
# Perl code
$sr &= $gf::field
// C# code
sr = sr & gf.field // or whatever you've named $gf::field
Edit following OP comment
OP asked whether:
if ($sr & (1<<$cwl))
Can be translated to:
cwl=1 << cwl;
if (Convert.ToBoolean(sr) & Convert.ToBoolean(cwl))
I think you've made an error here. cwl
is not reassigned in the original if
statement in Perl.
In fact, the logic of the operation is to say:
- Is
sr
bitwiseAND
1 left-shifted bycwl
true?
I'd be tempted to rewrite the Perl code to:
if ((sr & (1 << cwl)) != 0)
Why? The original statement ascertains whether the result of the if
statement is true
. Any non-zero value in Perl is true
, therefore we need to perform the operation as per the Perl and test for a non-zero value.
推荐阅读
- perl - perl 重载连接运算符(“.”)问题
- c# - 从数据库获取值到 Linq where 子句
- angular - Angular 7 从同级组件的子级滚动到动态 ngb-tab
- sql - 使用 With 语句将点值连接到多边形
- android - 我想知道同时启用了多少个 Switch 并打印它们的值
- php - phpDoc中应该如何使用@version?
- php - Cb 和 Cr 通道应该具有什么颜色才能使 RGB 中的像素变为红色?
- android - 使用 Instagram 的嵌入 url 时 Webview 无法显示
- powershell - 如何正确地将我的 try-catch 输出到 txt 文件中?
- svg.js - 是否可以将变换应用于 svg 元素?