我如何在 J 中编写这个 C 表达式?

发布于 2024-09-25 15:20:34 字数 519 浏览 2 评论 0原文

如何在 J 中编写此 C 表达式? (其中x是输入整数,a是临时变量)

((a= ~x & (~x >> 1)) ^= a ? 0 : (a ^ (a & (a - 1))) | (a ^ (a & (a - 1))) << 1);

编辑:

以更易读的形式:

    int a = (~x) & ((~x) >> 1);
    if (a == 0) return 0;
    int b = a ^ (a & (a - 1));
    return b | (b << 1);

How do I write this C expression in J? (where x is input integer, and a is temporary variable)

((a= ~x & (~x >> 1)) ^= a ? 0 : (a ^ (a & (a - 1))) | (a ^ (a & (a - 1))) << 1);

.

Edit:

In a more readable form:

    int a = (~x) & ((~x) >> 1);
    if (a == 0) return 0;
    int b = a ^ (a & (a - 1));
    return b | (b << 1);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

偷得浮生 2024-10-02 15:20:34

如果没有测试,基本的转录将是这样的:

Shift =: (33 b.)
And   =: (17 b.)
Not   =: (26 b.)
Xor   =: (22 b.)
Or    =: (23 b.)

BastardFunction =: 3 : 0
  a =. (Not y) And (_1 Shift (Not y))
  if. a do.
    b =. a  Xor (a And a - 1)
    (1 Shift b) Or b
  else.
    0
  end.
)

但可能有一种更聪明的方法。

Without testing, the basic transcription would be something like this:

Shift =: (33 b.)
And   =: (17 b.)
Not   =: (26 b.)
Xor   =: (22 b.)
Or    =: (23 b.)

BastardFunction =: 3 : 0
  a =. (Not y) And (_1 Shift (Not y))
  if. a do.
    b =. a  Xor (a And a - 1)
    (1 Shift b) Or b
  else.
    0
  end.
)

But there could be a smarter approach.

[旋木] 2024-10-02 15:20:34

这是一个小分析(“可读形式”版本):

usnigned int nx = ~x;   // I suppose it's unsigned 
int a = nx & (nx >> 1); 
// a will be 0 if there are no 2 consecutive "1" bits.
// or it will contain "1" in position N1 if nx had "1" in positions N1 and N1 + 1
if (a == 0) return 0;   // we don't have set bits for the following algorithm
int b = a ^ (a & (a - 1));  
// a - 1 : will reset the least 1 bit and will set all zero bits (say, NZ) that were on smaller positions
// a & (a - 1) : will leave zeroes in all (NZ + 1) LSB bits (because they're only bits that has changed
// a ^ (a & (a - 1)) : will cancel the high part, leaving only the smallest bit that was set in a
// so, for a = 0b0100100 we'll obtain a power of two: b = 0000100
return b | (b << 1);    
// knowing that b is a power of 2, the result is b + b*2 => b*3

算法似乎正在寻找 x0 位> 变量。
如果没有,则结果为 0。
如果找到它们,例如在位置 PZ 上,则结果将包含两个设置位:PZPZ+1

Here is a small analyzis (of the "readable form" version):

usnigned int nx = ~x;   // I suppose it's unsigned 
int a = nx & (nx >> 1); 
// a will be 0 if there are no 2 consecutive "1" bits.
// or it will contain "1" in position N1 if nx had "1" in positions N1 and N1 + 1
if (a == 0) return 0;   // we don't have set bits for the following algorithm
int b = a ^ (a & (a - 1));  
// a - 1 : will reset the least 1 bit and will set all zero bits (say, NZ) that were on smaller positions
// a & (a - 1) : will leave zeroes in all (NZ + 1) LSB bits (because they're only bits that has changed
// a ^ (a & (a - 1)) : will cancel the high part, leaving only the smallest bit that was set in a
// so, for a = 0b0100100 we'll obtain a power of two: b = 0000100
return b | (b << 1);    
// knowing that b is a power of 2, the result is b + b*2 => b*3

It seems that the algorithm is looking for the first 2 (starting from LSB) consecutive 0 bits in the x variable.
If there aren't any, then the result is 0.
If they are found, say on position PZ, then the result will contain two set bits: PZ and PZ+1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文