Using C language to realize parity-check __c language

Source: Internet
Author: User
Tags bitwise reserved

1. Parity check with C language

unsigned int v;  Number
bool parity = false to be detected;  The initial judgment mark while
(v)
{
parity =!parity;
v = v & (V-1);
}
2. Parity check by building dictionary table:

static const BOOL PARITYTABLE256[256] =
{
# define P2 (n) n, n^1, n^1, n
# define P4 (n) P2 (n), P2 (n^1), P2 (n^1), P2 (n)
# define P6 (n) P4 (n), P4 (n^1), P4 (n^1), P4 (n)
P6 (0), P6 (1), P6 (1), P6 (0)
};
By nesting the macro definition, make a sheet including 0~255 each number contains 1, which contains even number 1, then paritytable256[i]=0, otherwise paritytable256[i]=1;

If you want to determine the parity of the number of I in B of the char type, you can use the following code directly:

unsigned char b;
BOOL parity = paritytable256[b];
For the number of 32-bit, the following code is used:


unsigned int v;
V ^= v >> 16;
V ^= v >> 8;
BOOL parity = paritytable256[v & 0xFF];

Principle:


(1) by v^=v>>16, the lower 16 bits in V are bitwise-or (^) operations, equivalent to the parity of the total   number of 1 in the 0~16 with the total number of digits reserved 1;


(2) through v^=v>>8, a bitwise OR (^) operation of the 9~16 bit in V with the 0~8 bit, equivalent to the parity of the total number of 0~8 bits reserved for 1 and the total number of 1 in the 0~16;


(3) operates through (1) (2). The parity of the total number of 1 in the initial V is the same as the total number of 1 in the 1~8 bit in the last V, and the v&0xff is equivalent to getting 1 of the 1~8 bit in V and then checking the table.


or use the following code:

Unsigned char * p = (unsigned char *) &v;
parity = paritytable256[p[0] ^ p[1] ^ p[ 2] ^ p[3]];
Principle: Take the address of V and make the coercion type to char*,


e.g. v=1234


Binary as:


00000000 0000000000000100 11010010
p[0]:11010010


p[1]:00000100


p[2]:00000000


p[3]:00000000


    ^------------


       11010110


  through p[0] ^ p[1] ^ P[2] ^ P[3] Action, put all 1 in p[i] in a 8-digit number, and then check the table to

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.