Click here to Skip to main content
11,634,622 members (68,571 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C++
hi everyone
I want to compare 2 number with them for example 2 and 3
and my program do this operations with bitwise operators.
i dont know how write this program
help pls
Posted 27-Sep-12 22:15pm
Richard MacCutchan at 28-Sep-12 4:17am
What do you mean by "with bitwise operators", what result are you trying to obtain?
meysamp73 at 28-Sep-12 4:29am
& The AND operator
| The OR operator
^ The XOR operator
~ The Ones Complement or Inversion operator
>> The Right Shift operator
<< The Left Shift operator.

for example we receive 2 , 3 numbers from input and show 3 is greater than 2
Richard MacCutchan at 28-Sep-12 4:56am
Why not just use the math operators like > == etc.?
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

For unsigned numbers you may simply right shift the numbers, until their leading bit (MSB) is different. e.g.
(I'm pretty sure there are more efficient ways, have a look, if you can at "Hacker's Delight"[^] book).

#include <stdlib.h>
#include <stdio.h>

int compare(unsigned int a, unsigned int b);
int main()
  unsigned int a, b;
  int i;
  for (i = 1; i<20; i++)
    a =rand();
    b= rand();
    switch (compare(a,b))
    case -1:
      printf("%u is smaller than %u\n", a, b);
    case 0:
      printf("%u is equal to %u\n", a, b);
      case 1:
      printf("%u is greater than %u\n", a, b);
  return 0;
int compare(unsigned int a, unsigned int b)
  unsigned int fb = 1 << (sizeof(a)*8-1);
  while ( !( (a ^ b) & fb))
    a <<= 1;
    b <<= 1;
    if (!a && !b) return 0;
  if ( a & fb)
    return 1;
    return -1;
nv3 at 28-Sep-12 5:34am
Nice job. You just forgot to replace the < escape sequences which makes the source somewhat difficult to read.
CPallini at 28-Sep-12 5:41am
Thanks and sorry for the inconvenience, it should be OK now.
Malli_S at 28-Sep-12 6:51am
CPallini at 29-Sep-12 9:31am
Espen Harlinn at 29-Sep-12 6:57am
CPallini at 29-Sep-12 9:31am
Thank you.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

What about:
int integer1 = 666;
int integer2 = 666;
int result = integer1 & integer2; //bitwise AND
if(result == integer1)
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

bool equals_bitwise(int x, int y)
return x ^ y == 0;
bool differs_bitwise(int x, int y)
return x ^ y != 0;
CPallini at 28-Sep-12 6:24am
That's correct, but not answers the OP question.
armagedescu at 28-Sep-12 6:48am
Why not?
Richard MacCutchan at 28-Sep-12 9:41am
OP wants to find out which number is greater.
armagedescu at 28-Sep-12 10:35am
In mater of guessing you are right, but technically speaking I answered correctly to the question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 8,736
1 Sergey Alexandrovich Kryukov 8,359
2 Mika Wendelius 6,845
3 F-ES Sitecore 2,354
4 Suvendu Shekhar Giri 2,205

Advertise | Privacy | Mobile
Web01 | 2.8.150728.1 | Last Updated 28 Sep 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100