A - B in C language is equal to A+( ~B + 1 ), so ( ~B + 1 ) + B = 0.
Examples:
#include <cstdio>
using namespace std;
int main(void){
char A = 0x53;
char B = 0x9A;
char C;
C = A & B;
printf("%X\n", C);
C = A | B;
printf("%X\n", C);
C = ~A;
printf("%X\n", C);
C = ~B;
printf("%X\n", C);
C = A + (~A+1); // C = A - A;
printf("%X\n", C);
C = A ^ B;
printf("%X\n", C);
C = C ^ B;
printf("%X\n", C);
printf("Shifts.......\n");
C = A << 1;
printf("%X\n", C);
C = B << 1;
printf("%X\n", C);
C = A >> 1;
printf("%X\n", C);
C = B >> 1;
printf("%X\n", C);
A = 100;
A = A >> 1; // A = A / 2
printf("%d\n", A);
A<<=1; // A *= 2;
printf("%d\n", A);
return 0;
}
RESULT:
12
FFFFFFDB
FFFFFFAC
65
0
FFFFFFC9
53
Shifts.......
FFFFFFA6
34
29
FFFFFFCD
50
100
No comments:
Post a Comment