0

Why isnt there a bit-wise comparator like "====" in C++? Do i have to cast variables each time?

(note: i already know logical-arithmetic operators, looking a different thing. By the way there is not arithmetic-xor a^^b also :) I know i can check a&b against a(or b), a^b against zero,... )

#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;
}

Thanks.

I could have done:

__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...

Bloated code...

2
  • What should be the result of '7 === 4'?
    – Guy Adini
    Sep 5, 2012 at 7:13
  • 7====4 false because being not equal bit-wise Sep 5, 2012 at 7:13

4 Answers 4

2

Such an operator would not work, because it would be platform dependent.

I assume that 'x === y' should return false when x and y have different lengths, and since int has different lengths on different platforms, so would the result of this operation.

2

Do you really need to compare signed to unsigned?

You should redesign your algorithm to make both operands of == to have the same signedness.

3
  • When i go far in a project, sometimes changing a single variable affects many Sep 5, 2012 at 7:18
  • 1
    @tuğrulbüyükışık: It is still a design and not a language issue
    – Andriy
    Sep 5, 2012 at 7:22
  • Or define what he wants to compare. unsigned char is typically used for "raw memory", and there are cases where it might make sense to see if a byte in raw memory corresponds to a specific character. (The issue should be simple, but isn't, because the standard allows plain char to behave as if it were signed. Which means that something like 'é' may have a negative value, and never compare equal to an unsigned char. Sep 5, 2012 at 8:19
2

You can't do any comparisons on char or unsigned char. Integral promotion means that the smallest integral type you can compare is int; both the char and the unsigned char will be promoted to int before the comparison. (After which, on most modern machines, there will be no difference between bitwise comparison and value comparison.)

I'm tempted to say that you shouldn't want bit-wise comparison. In many ways, the implicit conversions (including the promotions) cause more problems than anything else, and logically, you should have to be explicit, and specify what you want to compare. (And the tests for equality are guaranteed to be "big-wise" if the type is unsigned char. This is why memcmp is defined in terms of unsigned char comparisons.)

4
  • When char(-1) promoted what it is in int? and unsigned char(255) ? Sep 5, 2012 at 8:56
  • Implementation defined. If plain char is signed, char(-1) is -1; if plain char is unsigned, char(-1) is 2^n-1, where n is the number of bits in char---255 on most modern machines. (unsigned char(255) is, of course, 255.) Sep 5, 2012 at 9:18
  • Then both of them promoted to int and one of them -1 the other one is 255 which are not same Sep 5, 2012 at 9:20
  • @tuğrulbüyükışık Both are promoted to int, without changing their value, but the value of char(-1) is implementation defined, and could be positive. Sep 5, 2012 at 9:31
1

You could do something like:

a ^ b == 0
5
  • I know this but i look for a single thing like a====b Sep 5, 2012 at 7:13
  • 1
    It doesn't exist, and frankly is not needed, you should do it as I suggested.
    – ronag
    Sep 5, 2012 at 7:14
  • 1
    This will still promote both values, and may yield false even if both have the same bit pattern. I think it's equivalent to a == b, just more obscure. Sep 5, 2012 at 7:21
  • @Mike Seymour is your last statement true? Sep 5, 2012 at 7:25
  • 4
    Yes it is. For example, try unsigned char a = 0xff; and signed char b = 0xff. Assuming int is larger than char, and signed arithmetic uses twos-complement, then they will be promoted to 0x00ff and 0xffff which will not appear equal using either == or ^. This answer is only useful if you want to obfuscate your code. Sep 5, 2012 at 7:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.