How can I check the parity of an integer variable (i.e. count the number of '1' bits), without resorting to inline assembly?
How can I check the parity of an integer variable (i.e. count the number of '1' bits), without resorting to inline assembly?
Something like
should work...Code:typedef enum {EVEN = 0, ODD = 1} parity_t; parity_t FindParity (unsigned int x) { parity_t parity; unsigned int temp = x; for (parity = EVEN; temp > 0; temp >>= 1) { parity ^= (temp & 0x1); } return parity; }
DavT
-----------------------------------------------
Do you mean to get the total number of set bits? That is to say:(i.e. count the number of '1' bits)
The parity of the following bit pattern "10100111" would return 5?
If so, you want:
That should suffice.Code:unsigned char parity( unsigned int O0O ) { int OO0, O00; for( OO0 = O00 = 0; OO0 < sizeof(unsigned int)*CHAR_BIT; OO0++ ) O00 += (!!(O0O & (1<<OO0))); return O00; }
Quzah.
Hope is the first step on the road to disappointment.
int findparity(unsigned int test){
int count=0;
int size;
size = sizeof(test);
size *= 8;
size--;
for(;size>=0;size--){
if((test>>size) & 0x01)
count++;
}
return count;
}
What about the systems with more then 8 bits per byte Karam?
Quzah you are an evil bastard sometimes
how is it possible? everyone knows that
1 byte=8 bits
nope sorry wrong. There are computers out there that have 16, 32, even 64 bit bytes.
>everyone knows that 1 byte=8 bits
I suppose the people who manufacture architectures where this isn't true haven't heard that yet. It's very likely that every system that you've worked with use an octet for the byte size, but you've not worked with every system that C is portable to, have you?
My best code is written with the delete key.
of course you need limits.h in quzah's code. I love that code quzah
Hmm, I was expecting someone to point out a less obvious way of doing it, instead of the pretty straightforward iteration along individual bits (which at first looked rather unelegant to me).
Say, how does a microprocessor implement this at register level? I know it probably isn't doable in C, but anyway I'm just wondering.
quzah: I hope your screen font is better than mine... it took me a while to check if you were actually right *g*Originally Posted by quzah
>I was expecting someone to point out a less obvious way of doing it
Well, we could suggest something, but that would require an unwarranted assumption about the size of an integer. To count all the set bits, a fairly efficient way that uses a loop is:
Arbitrarily choosing 32 bit integers, you could avoid the loop completely with something like this:Code:unsigned int bits( unsigned int val ) { int n = 0; while (val) { ++n; val &= val - 1; } return n; }
Then of course, there's the lookup table option. If you want speed, this is usually a good way to go.Code:unsigned int bits( unsigned int val ) { val = (val & 0x55555555) + ((val >> 1) & 0x55555555); val = (val & 0x33333333) + ((val >> 2) & 0x33333333); val = (val & 0x0f0f0f0f) + ((val >> 4) & 0x0f0f0f0f); val = (val & 0x00ff00ff) + ((val >> 8) & 0x00ff00ff); val = (val & 0x0000ffff) + ((val >> 16) & 0x0000ffff); return (int)value; }
My best code is written with the delete key.
You could have just compiled it. I was going to do this...Originally Posted by doshell
Code:unsigned char OxOO( unsigned int Ox0O ) { int OxO0,Ox00; for( OxO0=Ox00=0x00;OxO0<sizeof(unsigned int)*CHAR_BIT;OxO0=OxO0+0x01 ) Ox00=Ox00+(!!(Ox0O&(0x01<<OxO0))); return Ox00; }
Quzah.
Hope is the first step on the road to disappointment.
No.Originally Posted by Karam
1 octet = 8 bits
1 byte = the smallest addressable memory unit in your system. Can be 8 bits or more in C.
FYI, Texas DSP TMS320C64 bytes (hence char) have 16 bits.
Emmanuel Delahaye
"C is a sharp tool"
This is really old-fashioned. But works.
Code:#define PARITY ( \ bits.bitmap.bit0 + \ bits.bitmap.bit1 + \ bits.bitmap.bit2 + \ bits.bitmap.bit3 + \ bits.bitmap.bit4 + \ bits.bitmap.bit5 + \ bits.bitmap.bit6 + \ bits.bitmap.bit7 ) unsigned int parity(const unsigned char mychar){ struct bmap{ /* bitfields for 8 bit byte */ unsigned bit7:1; /* 8 bit char */ unsigned bit6:1; unsigned bit5:1; unsigned bit4:1; unsigned bit3:1; unsigned bit2:1; unsigned bit1:1; unsigned bit0:1; }; union b_bits{ /* allow moving a byte into the bitfields*/ unsigned char ch; struct bmap bitmap; } bits; bits.ch=mychar; return PARITY; }
There is nothing in C which states that sizeof(struct bmap) == sizeof(unsigned char)
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.