XNOR(异或非)门是一种数字逻辑门,它接受两个输入并给出一个输出。其功能是异或(XOR)门的逻辑补。如果两个输入相同,则输出为 TRUE;如果输入不同,则输出为 FALSE。下面给出了异或非门的真值表。
一个 B 输出111100010001
问题陈述
给定两个数字 x 和 y。求两个数的异或。
示例示例1
Input: x = 12, y = 5
登录后复制登录后复制登录后复制
Output: 6
登录后复制登录后复制登录后复制
说明
(12)10 = (1100)2(5)10 = (101)2XNOR = (110)2 = (6)10
登录后复制
Sampe Example 2
的中文翻译为:
示例示例2
Input: x = 16, y = 16
登录后复制
Output: 31
登录后复制
说明
(16)10 = (10000)2(16)10 = (10000)2XNOR = (11111)2 = (31)10
登录后复制
方法一:暴力法
暴力方法是检查两个数字的每一位并比较它们是否相同。如果相同则加1,否则加0。
伪代码
procedure xnor (x, y) if x > y then swap(x,y) end if if x == 0 and y == 0 then ans = 1 end if while x x_rem = x & 1 y_rem = y & 1 if x_rem == y_rem then ans = ans | (1 > 1 y = y >> 1end procedure
登录后复制
示例:C++ 实现
在下面的程序中,检查x和y的位是否相同,然后设置答案中的位。
#include using namespace std;// Function to swap values of two variablesvoid swap(int *a, int *b){ int temp = *a; *a = *b; *b = temp;}// Function to find the XNOR of two numbersint xnor(int x, int y){ // Placing the lower number in variable x if (x > y){ swap(x, y); } // Base Condition if (x == 0 && y == 0){ return 1; } // Cnt for counting the bit position Ans stores ans sets the bits of XNOR operation int cnt = 0, ans = 0; // executing loop for all the set bits in the lower number while (x){ // Gets the last bit of x and y int x_rem = x & 1, y_rem = y & 1; // If last bits of x and y are same if (x_rem == y_rem){ ans |= (1 > 1; y = y >> 1; } return ans;}int main(){ int x = 10, y = 11; cout输出
XNOR of 10 and 11 = 14登录后复制
时间复杂度:O(logn),因为遍历是对所有 logn 位进行的。
空间复杂度:O(1)
方法二
XNOR是XOR运算的逆运算,它的真值表也是XOR表的逆运算。因此,切换较高数字的位,即将 1 设置为 0,将 0 设置为 1,然后与较低数字进行异或将产生 XNOR 数字。
示例 1
Input: x = 12, y = 5登录后复制登录后复制登录后复制
Output: 6登录后复制登录后复制登录后复制
说明
(12)10 = (1100)2(5)10 = (101)2Toggled bits of 12 = 00110011 ^ 101 = 0110登录后复制
Sampe Example 2
的中文翻译为:
示例示例2
Input: x = 12, y = 31登录后复制登录后复制
Output: 12登录后复制登录后复制
说明
(12)10 = (1100)2(31)10 = (11111)2Toggled bits of 31 = 0000000000 ^ 1100 = 01100登录后复制
伪代码
procedure toggle (n) temp = 1 while temp示例:C++ 实现
在下面的程序中,较高数字的所有位都会被切换,然后与较低数字进行异或。
#include using namespace std;// Function to toggle all bits of a numbervoid toggle(int &num){ int temp = 1; // Execute loop until set bit of temp cross MST of num while (temp输出
XNOR of 5 and 15 = 5登录后复制
时间复杂度:O(logn),由于在toggle()函数中进行遍历
空间复杂度:O(1)
方法 3:位掩码
从逻辑上讲,XNOR是XOR的反向操作,但是在对XOR进行补码操作时,前导零也会被反转。为了避免这种情况,可以使用位掩码来去除所有不必要的前导位。
示例示例1
Input: x = 12, y = 5登录后复制登录后复制登录后复制
Output: 6登录后复制登录后复制登录后复制
说明
(12)10 = (1100)2(5)10 = (101)21100 ^ 101 = 1001Inverse of 1001 = 0110登录后复制
示例 2
Input: x = 12, y = 31登录后复制登录后复制
Output: 12登录后复制登录后复制
说明
(12)10 = (1100)2(31)10 = (11111)21100 ^ 11111 = 10011Inverse of 10011 = 01100登录后复制
伪代码
Procedure xnor (x, y) bit_count = log2 (maximum of a and y) mask = (1示例:C++ 实现
在下面的程序中,位掩码用于从 x xor y 的逆中仅获取所需的位。
#include using namespace std;// Function to find the XNOR of two numbersint xnor(int x, int y){ // Maximum number of bits used in both the numbers int bit_count = log2(max(x, y)); int mask = (1输出
XNOR of 10 and 10 = 7登录后复制
结论
总之,可以使用各种方法和时间复杂度来找到两个数字的XNOR,范围从O(logn)的暴力法到O(1)的最优化方法。应用位运算更加廉价,因此暴力法的复杂度是对数级别的。
以上就是两个数字的XNOR的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2582660.html