为什么在 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...
臃肿的代码...