Write a C program to find the parity of an unsigned integer
来源:互联网 发布:淘宝买家信誉等级表 编辑:程序博客网 时间:2024/05/16 11:13
reference:
http://www.geeksforgeeks.org/write-a-c-program-to-find-the-parity-of-an-unsigned-integer/
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;}
- Write a C program to find the parity of an unsigned integer
- Write an Efficient C Program to Reverse Bits of a Number
- Write a function to find the longest common common string amongst an array of strings.
- Write a function to find the longest common prefix string amongst an array of strings.
- 4、Write a function to find the longest common prefix string amongst an array of strings
- 【NO.5】c program to find factorial of a number.
- Given an integer, write a function to determine if it is a power of two.
- Given an integer, write a function to determine if it is a power of two
- How to find number of a specific integer in an array of Fixnums ruby
- Write a C program to print all permutations of a given string
- Six Steps to write an ILE C CGI program
- Write a function to find the depth of a binary tree
- Write a function to find the depth of a binary tree
- leetCode Given an integer (signed 32 bits), write a function to check whether it is a power of 4
- How is jstack being used to find the bottlenect of a java program
- Write a method to print the last K lines of an input file using C++
- Reverse bits of an unsigned integer
- Write a program that prints the numbers from 1 to 100,but for multiples of three print “Fizz” inste
- java.util.Date和java.sql.Date的区别及应用
- sql date format
- sae-python相关工具
- Android 软件版本号
- 应用决定产品
- Write a C program to find the parity of an unsigned integer
- linux uname命令参数及用法详解--linux查看系统信息命令
- word粘贴至html特殊字符 粘贴后可能为乱码
- 程序员面试题--奇虎360
- LayoutInflater.from(this) View.inflate()
- 客户管理案例
- Ubuntu13.04 显示隐藏文件和文件夹
- 【explorer崩溃】手动修复指南
- Java中单例模式的几种正确实现方法