Write a C program to find the parity of an unsigned integer

2024-02-14 17:48

本文主要是介绍Write a C program to find the parity of an unsigned integer,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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 = 0
2. Loop while n != 0      a. Invert parity parity = !parityb. Unset rightmost set bitn = n & (n-1)
3. return parity


Code:

/* Function to get parity of number n. It returns 1if n has odd parity, and returns 0 if n has evenparity */
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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/709178

相关文章

Unstructured cannot write mode RGBA as JPEG 错误解决

Unstructured cannot write mode RGBA as JPEG 错误解决 0. 错误详细1. 解决方法 0. 错误详细 Image Extraction Error: Skipping the failed imageTraceback (most recent call last):File "/root/miniconda3/envs/learn-y

MongoDB学习—(6)MongoDB的find查询比较符

首先,先通过以下函数向BookList集合中插入10000条数据 function insertN(obj,n){var i=0;while(i<n){obj.insert({id:i,name:"bookNumber"+i,publishTime:i+2000})i++;}}var BookList=db.getCollection("BookList")调用函数,这样,BookList

70-java write类应用场景

在Java中,我们可以使用java.io包中的FileWriter和BufferedWriter类来写入数据到文件。以下是一个简单的例子,展示了如何使用FileWriter和BufferedWriter来写入数据到文件: import java.io.BufferedWriter;import java.io.FileWriter;import java.io.IOException;pub

【NodeJS】Error: Cannot find module 'ms'

转载自:http://blog.csdn.net/echo_ae/article/details/75097004 问题: Error: Cannot find module 'ms'at Function.Module._resolveFilename (module.js:469:15)at Function.Module._load (module.js:417:25)at Module

leetCode#448. Find All Numbers Disappeared in an Array

Description Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this

访问controller404:The origin server did not find a current representation for the target resource

ider build->rebuild project。Rebuild:对选定的目标(Project),进行强制性编译,不管目标是否是被修改过。由于 Rebuild 的目标只有 Project,所以 Rebuild 每次花的时间会比较长。 参考:资料

mybatis错误——java.io.IOException Could not find resource comxxxxxxMapper.xml

在学习Mybatis的时候,参考网上的教程进行简单demo的搭建,配置的没有问题,然后出现了下面的错误! Exception in thread "main" java.lang.RuntimeException: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause:

SylixOS write 0 字节问题

1 问题描述 在移植中间件过程中,在SylixOS调用write函数写入0字节的数据到文件中时,会导致对应的中间件测试用例失败,失败的原因是文件系统中的write函数在Linux系统和SylixOS有区别,两种实现的差别如下。 2 write函数的实现机制 2.1 SylixOS实现机制 在SylixOS下通过write 函数写数据到普通文件中时,第一步会判断写入的数据是否为0,如果是0直

启动读取program arguments参数

1,有时我们在启动项目的时候配置了program arguments但是却读取不到,这时用下面的方式启动则可以,我把所有的相关代码都放在了下面,如果觉得没有用的大家可在自己的项目中删除,只是项目启动的时候有点变化 启动主方法 @MapperScan({ "com.kidy.mapper" }) @SpringBootApplication public class RunApplication

Java 入门指南:Java 并发编程 —— Copy-On-Write 写时复制技术

文章目录 Copy-On-Write使用场景特点缺点CopyOnWrite 和 读写锁相同点之处不同之处 CopyOnWriteArrayList适用场景主要特性方法构造方法CopyOnWriteArrayList 使用示例 CopyOnWriteArraySet适用场景主要特性方法构造方法使用注意事项CopyOnWriteArraySet 使用示例 Copy-On-Writ