博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Write a C program to find the parity of an unsigned integer
阅读量:4151 次
发布时间:2019-05-25

本文共 707 字,大约阅读时间需要 2 分钟。

reference: 

Problem Definition:

Parity: Parity of a number refers to whether it contains an odd or even number of 1-bits. The number has “odd parity”, if it contains odd number of 1-bits and is “even parity” if it contains even number of 1-bits.

Solution:

1. Initialize parity = 02. Loop while n != 0            a. Invert parity              parity = !parity      b. Unset rightmost set bit             n = n & (n-1)3. return parity

Code:

/* Function to get parity of number n. It returns 1   if n has odd parity, and returns 0 if n has even   parity */bool getParity(unsigned int n){    bool parity = 0;    while (n)    {        parity = !parity;        n      = n & (n - 1);    }            return parity;}

转载地址:http://bexti.baihongyu.com/

你可能感兴趣的文章
C++中异常的处理方法以及使用了哪些关键字
查看>>
如何定义和实现一个类的成员函数为回调函数
查看>>
内存分配的形式有哪些? C++
查看>>
什么是内存泄露,如何避免内存泄露 C++
查看>>
栈和堆的空间大小 C++
查看>>
什么是缓冲区溢出 C++
查看>>
sizeof C++
查看>>
使用指针有哪些好处? C++
查看>>
引用还是指针?
查看>>
checkio-non unique elements
查看>>
checkio-medium
查看>>
checkio-house password
查看>>
checkio-moore neighbourhood
查看>>
checkio-the most wanted letter
查看>>
Redis可视化工具
查看>>
大牛手把手带你!2021新一波程序员跳槽季,全套教学资料
查看>>
JAVA自定义注解与通过反射去解析注解参数
查看>>
Effective Java学习(创建和销毁对象)之——通过私有化构造器强化不可实例化的能力...
查看>>
Effective Java学习(创建和销毁对象)之——消除过期对象引用
查看>>
Effective Java学习(泛型)之——消除非受检警告
查看>>