一个词的奇偶校验 [英] Parity check of a word
问题描述
大家好,
是否有一种简单快捷的方法来检查一个单词的奇偶校验(例如,一个
4字节整数)?那就是我想知道它们的数量是否为
偶数或奇数。
当然我可以用一些额外的代码来做,但我想应该
至少是一个ASM命令? (英特尔)
感谢您的帮助!
问候
Herbert
Hi everyone,
is there a simple and fast method to check the parity of a word (e. g. a
4-byte integer)? That is I want to know whether the number of ones are
even or odd.
Of course I could do that with some extra code, but I guess there should
be at least an ASM command? (Intel)
Thanks for any help!
Regards
Herbert
推荐答案
在文章< pa **************************** @ localhost.localdom ain>,
Herbert Haas< hh@localhost.localdomain>写道:
In article <pa****************************@localhost.localdom ain>,
Herbert Haas <hh@localhost.localdomain> wrote:
是否有一种简单快速的方法来检查单词的奇偶校验(例如,一个4字节的整数)?那就是我想知道它们的数量是偶数还是奇数。
当然我可以用一些额外的代码来做到这一点,但我想至少应该是一个ASM命令? (英特尔)
is there a simple and fast method to check the parity of a word (e. g. a
4-byte integer)? That is I want to know whether the number of ones are
even or odd. Of course I could do that with some extra code, but I guess there should
be at least an ASM command? (Intel)
机器级或汇编程序功能超出了范围
of comp.lang.c。
>
简单而快速......总是有查找表和小循环。
-
Who Leads? " /男人必须......驱使男人,强迫男人。
怪胎男人。
你们都是怪胎,先生。但你一直都是怪胎。
生活是一个怪胎。这是它的希望和荣耀。 - Alfred Bester,TSMD
Machine level or assembler capabilities are beyond the scope
of comp.lang.c .
"Simple and fast"... there''s always look-up tables and small loops.
--
"Who Leads?" / "The men who must... driven men, compelled men."
"Freak men."
"You''re all freaks, sir. But you always have been freaks.
Life is a freak. That''s its hope and glory." -- Alfred Bester, TSMD
Herbert Haas< hh@localhost.localdomain>写道:
Herbert Haas <hh@localhost.localdomain> writes:
是否有一种简单快速的方法来检查单词的奇偶校验(例如,一个4字节的整数)?那就是我想知道它们的数量是偶数还是奇数。
is there a simple and fast method to check the parity of a word (e. g. a
4-byte integer)? That is I want to know whether the number of ones are
even or odd.
根据书中的代码_Hacker'Delight_(任何错误)
mine):
/ *返回X的奇偶校验。* /
int parity(unsigned x)
{
x ^ = x>> 1;
x ^ = x>> 2;
x ^ = x>> 4;
x ^ = x>> 8;
x ^ = x>> 16;
返回x;
}
-
int main(void){char p [] =" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz。\
\ n",* q =" kl BIcNBFr.NKEzjwCIxNJC" ;; int i = sizeof p / 2; char * strchr(); int putchar(\\ \\ b
); while(* q){i + = strchr(p,* q ++) - p; if(i> =(int)sizeof p)i- = sizeof p-1; putchar( p [i] \
);}返回0;}
Based on code from the book _Hacker''s Delight_ (any errors are
mine):
/* Returns parity of X. */
int parity (unsigned x)
{
x ^= x >> 1;
x ^= x >> 2;
x ^= x >> 4;
x ^= x >> 8;
x ^= x >> 16;
return x;
}
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
" Herbert Haas" < hh@localhost.localdomain>在留言中写道
news:pa **************************** @ localhost.loca ldomain ...
"Herbert Haas" <hh@localhost.localdomain> wrote in message
news:pa****************************@localhost.loca ldomain...
大家好,
是否有一种简单快速的方法来检查单词的奇偶校验(例如,一个4字节的整数)?那就是我想知道它们的数量是偶数还是奇数。
当然我可以通过一些额外的代码来做到这一点,但我想应该是
至少ASM命令? (英特尔)
Hi everyone,
is there a simple and fast method to check the parity of a word (e. g. a
4-byte integer)? That is I want to know whether the number of ones are
even or odd.
Of course I could do that with some extra code, but I guess there should
be at least an ASM command? (Intel)
我们这里只讨论标准C,而不是汇编语言。
这是C中的一个简单例子:
#include< limits.h>
#include< stdio.h>
unsigned int bits(unsigned int value)
{
unsigned int result = 0;
unsigned int mask = 1;
而(面具)
{
结果+ =(价值&面具)!= 0;
mask * = 2;
}
返回结果;
}
unsigned int even(unsigned int value)
{
return!(bits(value)%2);
}
unsigned int odd(unsigned int value)
{
return!even(value);
}
int main()
{
const char * parity [] = {" ODD"," EVEN" };
unsigned int i = 0;
puts(Value Bits Parity);
对于( ;我< 20; ++ i)
printf("%5u%4u%s \ n",i,bits(i),parity [even(i)]);
>
返回0;
}
请注意,上述方法仅适用于无符号值。
-Mike
We only discuss standard C here, not assembly language.
Here''s a simple example in C:
#include <limits.h>
#include <stdio.h>
unsigned int bits(unsigned int value)
{
unsigned int result = 0;
unsigned int mask = 1;
while(mask)
{
result += (value & mask) != 0;
mask *= 2;
}
return result;
}
unsigned int even(unsigned int value)
{
return !(bits(value) % 2);
}
unsigned int odd(unsigned int value)
{
return !even(value);
}
int main()
{
const char *parity[] = {"ODD", "EVEN"};
unsigned int i = 0;
puts("Value Bits Parity");
for(; i < 20; ++i)
printf("%5u %4u %s\n", i, bits(i), parity[even(i)]);
return 0;
}
Note that the above method will only work for unsigned values.
-Mike
这篇关于一个词的奇偶校验的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!