计算 Ruby 中字节的奇偶校验
在 Ruby 中计算一个字节是否具有奇数或偶数奇偶校验的最佳方法是什么?我有一个版本可以工作:
result = "AB".to_i(16).to_s(2).count('1').odd?
=> true
将数字转换为字符串并计算“1”似乎是计算奇偶校验的一种糟糕方法。还有更好的方法吗?
我希望能够计算 3DES 密钥的奇偶校验。最终,我想将偶数字节转换为奇数字节。
谢谢, 担
What's the best way to calculate if a byte has odd or even parity in Ruby? I've got a version working:
result = "AB".to_i(16).to_s(2).count('1').odd?
=> true
Converting a number to a string and counting the "1"s seems a poor way of calculating parity though. Any better methods?
I want to be able to calculate the parity of a 3DES key. Eventually, I'll want to convert even bytes to odd.
Thanks,
Dan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
除非你现有的速度不够快,否则就保留它。清晰简洁,性能比你想象的还要好。
我们将针对数组查找(我测试过的最快方法)对所有内容进行基准测试:
Unless what you have is not fast enough, keep it. It's clear and succinct, and its performance is better than you think.
We'll benchmark everything against array lookup, the fastest method I tested:
您是否看过 RubyDES 库?这可能会消除您自己编写实现的需要。
要计算奇偶校验,您可以使用如下内容:
根据简单的基准测试,内联 c 版本比本机 ruby 版本快 3-4 倍
Have you taken a look at the RubyDES library? That may remove the need to write your own implementation.
To calculate parity, you can use something like the following:
According to simple benchmarks, the inline c version is 3-4 times faster than the native ruby version
具有 255 个条目的数组查找表可能是最快的“Ruby”解决方案。
在 CI 中会屏蔽和转移。或者,如果我有 SSE4,我会使用带有内联汇编器的 POPCNT 指令。如果您需要高性能,请用 C 编写一个本机扩展来执行上述任一操作。
http://en.wikipedia.org/wiki/SSE4
Probably a lookup table of an Array with 255 entries would be fastest "In Ruby" solution.
In C I would mask and shift. Or if I have SSE4 I would use the POPCNT instruction with inline assembler. If you need this to be high performance write a native extension in C which does either of the above.
http://en.wikipedia.org/wiki/SSE4
将您的原始解决方案与记忆一起使用怎么样?这只会为每个整数值计算一次。
How about using your original solution with memoization? This will only calculate once for each integer value.
可以缩短为☺
如果你想要一些不可读的东西,
which can be shortened to
if you want something that is unreadable ☺
我将构建一个包含 16 个条目的表(作为 16 个字符的表),对应于字节的每个半字节。条目为 0,1,1,2,1,2,....4
要测试您的字节,
请屏蔽左侧半字节并进行查找,记住数字。
做。右移 4 并进行第二次查找,将结果数字与前一个数字相加以提供总和。
然后测试总和的低位。如果为1,则该字节为奇数,如果为0,则该字节为偶数。如果结果为偶数,则使用异或指令翻转高位。
这种查找方法比通过单次移位将字节中的位相加要快得多。
请给我发电子邮件,索要一个简单的函数来执行 8 字节的奇偶校验。 3DES 使用 3 组 8 字节。
I would construct a single table of 16 entries (as a 16 character table), corresponding to each nibble (half) of a bytes. Entries are 0,1,1,2,1,2,....4
To test your byte,
Mask out the left nibble and do a lookup, memorizing the number.
Do. a shift to the right by 4 and do a second lookup, adding the result number to the previous one to provide a sum.
Then test the low order bit from the sum. If it is 1, the byte is odd, if it is a 0, the byte is even. If result is even, you flip the high order bit, using the xor instruction.
THis lookup method is much faster than adding up the bits in a byte by single shifts.
email me for a simple function to do the parity for 8 bytes. 3DES uses 3 groups of 8 bytes.