troubleshooting Question

The bitwise OR in C

Avatar of CroetOne
CroetOneFlag for Netherlands asked on
C
11 Comments1 Solution171 ViewsLast Modified:
Hi there, I'm new in C and using a downloaded tutorial to practice and learn. Heading up to the 17th chapter I have a question I can't find an answer to.
The exercise is showing me the use of the bitwise OR:
#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 OR
printf("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?
What do i have to change to let the bitwise OR give a result higher then 127?
ASKER CERTIFIED SOLUTION
Zoppo

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 11 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 11 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros