[英]How to Compare the Numerical value of the Second-lowest Byte of L to the Second Lowest Byte of M
How to Compare the Numerical value of the Second-lowest Byte of L to the Second Lowest Byte of M?如何比较 L 的第二低字节与 M 的第二低字节的数值?
I have a pseudo-random number generator, but I am unsure how to sort the bytes.我有一个伪随机数生成器,但我不确定如何对字节进行排序。 I need to find the second lowest byte of "L" and "M" and then increment the value of the variable "lHigh" or "mHigh" depending on which one has the greater "second-lowest" byte.我需要找到“L”和“M”的第二低字节,然后根据哪个具有更大的“第二低”字节来增加变量“lHigh”或“mHigh”的值。
So, for example, the second-lowest Byte of the 32-bit Hex number 0xAABB CC DD would be the “CC” value.因此,例如,32 位十六进制数 0xAABB CC DD 的第二低字节将是“CC”值。
If L is higher than M i would want to increment the variable "lHigh"如果 L 高于 M 我想增加变量“lHigh”
So basically, I am comparing bits 8-15 of L to bits 8-15 of M.所以基本上,我将 L 的 8-15 位与 M 的 8-15 位进行比较。
With unsigned values and lowest means least significant : the 2nd least significant bytes are使用无符号值和最低表示最低有效:第二个最低有效字节是
#include <limits.h>
unsigned char L2 = L >> CHAR_BIT;
unsigned char M2 = M >> CHAR_BIT;
// Form a 1,0,-1 depending on compare result.
int compare = (L2 > M2) - (L2 < < M2);
To elminate the bits of the "lowest byte" so they have no consequence you can 'knock those bits down' by AND
ing with the one's complement
of 0xFF.要消除“最低字节”的位,使它们没有任何后果,您可以通过AND
与 0xFF one's complement
一起“敲低这些位”。
( x & ~0xFF )
Below, two integer values are shown and then compared.下面显示了两个 integer 值,然后进行比较。
int mmain() {
// test 5 values crossing a threshold
for( int i = 0; i < 5; i++ ) {
int x = (1 << 8) + 254; // a start with 1 in "2nd lowest byte"
int y = (1 << 8) + 4;
x += i; // "lowest byte" of 'x' increases on each loop
// show values
printf( "%d : x(%d) y(%d) :: special-x(%d) special-y(%d) :: ",
i, x, y, x&~0xFF, y&~0xFF );
// show comparison
puts( (x&~0xFF) == (y&~0xFF) ? "x==y" : "x > y" );
}
return 0;
}
Output Output
0 : x(510) y(260) :: special-x(256) special-y(256) :: x==y
1 : x(511) y(260) :: special-x(256) special-y(256) :: x==y
2 : x(512) y(260) :: special-x(512) special-y(256) :: x > y
3 : x(513) y(260) :: special-x(512) special-y(256) :: x > y
4 : x(514) y(260) :: special-x(512) special-y(256) :: x > y
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.