#include <stdio.h>
#define SET 32
char *binbin(int n);
int main()
{
int bor, result;
printf("Type a value from 0 to 255: ");
scanf("%d",&bor);
result = bor | SET;
printf("\t%s\t%d\n",binbin(bor),bor);
printf("|\t%s\t%d\n",binbin(SET),SET);
printf("=\t%s\t%d\n",binbin(result),result);
return 0;
}
char *binbin(int n)
{
static char bin[9];
int x;
for (x=0;x<8;x++)
{
bin[x] = n & 0X80 ? '1' : '0';
n << 1;
}
bin[x]='\0';
return(bin);
}
When I run this and I enter a value of less or equal to 95, I get this result:Type a value from 0 to 255: 95
01011111 95
| 00100000 32
= 01111111 127
When I enter a value greater then 95, the outcome is:Type a value from 0 to 255: 96
01100000 96
| 00100000 32
= 01100000 96
In first I thought the binbin function is giving the wrong output. To test that I added the next line directly after the bitwise ORprintf("This is the result from bitwise OR: %d\n",result);
Running again, it's now clear that the outcome of the bitwise OR can't get higher then 127.
result = bor | SET;
I do understand the 127 in relation to the binary context, but how thus this limitation exist?Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.
When asked, what has been your best career decision?
Deciding to stick with EE.
Being involved with EE helped me to grow personally and professionally.
Connect with Certified Experts to gain insight and support on specific technology challenges including:
We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE