For an int
If you want to clear the MSB of an int x;
use x &=7fffffff;
(removes only first bit)
For a short int
If you want to clear the MSB of a short x;
use x &=7fff;
(removes only first bit)
if short x == 1001011111001010
and
short mask == 0111111111111111;
then
x &= mask;
==0001011111001010
(or 0x18ca)
However, given you have: 0x1b3, (or 0x1da), but desire 0xb3 (or 0xda) if appears you want to mask the first two bytes.
In that case, set your mask to 0x00ff
It appears you are working in short int
, change your code then from int
to short
, or (as @wildplasser comments) to unsigned short
.
The code example you posted has unreachable code in several places, and does not include a return statement:
int galois_multiply( int a,int b){
int flag,res;
switch (a){
case 1:
return b;
break;//unreachable code
case 2: flag= b * 0x80;
b= b << 1; //left shift
if (flag)
res= b ^ 0x1b;
else
res= b ^0x00;
printf("\nnumber %d returned by galois_multiply function\n",res);
return res;
break;//unreachable code
case 3: res= b ^ galois_multiply(2,b);
printf("\nnumber %d returned by galois_multiply function\n",res);
return res;
break;//unreachable code
default:
printf("Invalid number %d passed to galois_multiply function\n",a);
exit(EXIT_FAILURE);
}
//no return statement (prototype specifies one)
}
The switch()
statement does not require break;
statements when preceded with some other exit method, such as return x;