在二进制数位Filpping [英] Filpping bits in binary number

查看:142
本文介绍了在二进制数位Filpping的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以对下面的问题提供了一个清晰的逻辑。我坚持混乱。

Can anyone provide a clear logic for the below problem. I am stuck with confusion.

的n比特数被给定为输入和OP(j)和OP(k)的一前一后施加在其上。目标是指定施加这两个操作后多少位都将保持不变。

An n bit number is given as input and OP(j) and OP(k) are applied on it one after the other. Objective is to specify how many bits will remain the same after applying these two operations.

OP(我)隐含的第i个的位翻转。 I> 0

OP(i) implies flipping of each ith bit. i > 0

推荐答案

根据您的描述,操作OP(我)会改变每一个第i位,所以它的变化,总楼面的(N / I)位。链接操作使事情变得棘手。如果相同的参数在两次通过,那么相同的值将被翻转,翻转比特的总数是0,因为我们恢复为原来的值。

Based on your description, the operation OP(i) will change every 'i'th bit, so it changes a total of floor(n / i) bits. Chaining the operation makes things tricky. If the same parameter is passed in twice, then the same values will be flipped, the overall number of flipped bits is 0 since we revert to the original value.

如果第二次手术采用的是不同的值(J),那么你需要添加地板(N / J),然后减去2 * M,其中M是n范围内i和j的公倍数的数量。这是因为你会被翻动任何公倍数两次,并恢复他们回到其原始值,但(一旦OP(i)和曾在OP(J))已经积累了他们两次,你需要从总减去2考虑到它。

If the second operation uses a different value (j), then you need to add floor(n / j) and then subtract 2*M, where M is the number of common multiples of i and j within the n range. This is because you'll be flipping any common multiples twice and reverting them back to their original value, but having accumulated them twice (once in OP(i) and once in OP(j)), you need to subtract 2 from the total to account for it.

这篇关于在二进制数位Filpping的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆