Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is bitwise OR operation not working as expected when an unsigned char is ORed with 11100000?

I am not able to understand why the operation ‘c | 11100000’ does not seem to work. But I also noticed that ‘c | 10000000’ works as expected.

#include <stdio.h>

int main()
{
    unsigned char c, c1;
    
    c = c & 0;
    c = c | 11100000;
    printf("%o \t", c);
    
    /** prints 140 ***/
    
    
    c = c & 0;
    c = c | 111;
    c << 5;
    printf("%o", c);
    
    /** prints 157 **/

    return 0;
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The constant values that you are using are in decimal format, not binary.

C doesn’t support binary constants, but it does support hex constants:

c = c | 0xe0;
...
c = c | 0x7;

Also, this doesn’t do anything:

c << 5;

Presumably, you want:

c = c << 5;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading