gpt4 book ai didi

c++ - C++中char和unsigned char的按位比较

转载 作者:太空宇宙 更新时间:2023-11-04 15:06:51 26 4
gpt4 key购买 nike

<分区>

为什么 C++ 中没有像“====”这样的按位比较器?我每次都必须转换变量吗?

(注意:我已经知道逻辑算术运算符,看起来不同。顺便说一下,也没有算术异或 a^^b :)我知道我可以检查 a&b 与 a(或 b),a^b 与零,...)

#include "stdafx.h"
#include<iostream>
#include<stdlib.h>

int main()
{
char a=-1;
unsigned char b=0;

for(b=0;b<255;b++)
{
if(a==b)std::cout<<(int)b; //cannot find 11111111)2==11111111)2
//ok, it looks -1==255 surely
//if(a====b)std::cout<<" bitwise equal "; could be good
}



bool compare=true;
bool bit1=false,bit2=false;
unsigned char one=1;

b=255; //bit-swise equal to a
for(int i=1;i<7;i++)
{
bit1=(a>>i)&one;
bit2=(b>>i)&one;
if(bit1!=bit2){compare=false;return;}//checks if any bit is different
}

if(compare)std::cout<<(int)b; //this writes 255 on screen

getchar();
return 0;
}

谢谢。

我本可以做到:

__asm
{
push push....
mov al,[a]
mov dl,[b]
cmp al,dl //this is single instr but needs others
je yes
mov [compare],false
jmp finish
yes:
mov [compare],true
jmp finish

finish:
pop pop...

臃肿的代码...

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com