0% found this document useful (0 votes)
12 views

operators bitwise predict

Uploaded by

Ananthi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

operators bitwise predict

Uploaded by

Ananthi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Predict the output of following programs

Program - 1
#include <stdio.h>
int main()
{
unsigned char a=0xAA;
unsigned char b=0;

b= (a&0x0F);

printf("b= %02X\n",b);

return 0;
}

Output
b= 0A

Explanation

In the statement (a&0x0F) bit masking is applying on the a, it will return first 4 bits (from 0 to 3)
of the a, thus the output will be 0A [in Binary 0000 1010].

Program - 2
#include <stdio.h>
int main()
{
unsigned char a=0xAA;

if(a & 0x01)


printf("one - true\n");
else
printf("one - false\n");

if(a & 0x02)


printf("two - true\n");
else
printf("two - false\n");

return 0;
}

Output
one - false
two - true

Explanation

Bitwise AND '&' returns true if specified bit is set (high), here binary value of a will be (1010
1010), thus, statement (a & 0x01) will return false, because first bit of the a is low (0) and the
statement (a & 0x02) will return true because second bit of the a is set (1).

Program - 3
#include <stdio.h>
int main()
{
unsigned char a=0xAA;

printf("a= %02X\n",a);

if(a | 0x01)
printf("true\n");
else
printf("false\n");

return 0;
}

Output
a= AA
true

Explanation

Bitwise OR '|' operator returns true, if any bit (from both operands, on specific position) is set
(1)/high. In the statement (a | 0x01) [ in Binary: 1010 1010 & 0000 0001] first bit of second
operand is true, thus, this statement will return 1 and condition will be true.

Program - 4
#include <stdio.h>
int main()
{
unsigned char a=0x00;
a= a|0x01;
a= a|0x02;
a= a|0x04;
a= a|0x08;

printf("a= %02X\n",a);

return 0;
}

Output
a= 0F

Explanation

Here, bitwise OR operator will add the particular bit by 1.


a= a|0x01; [Here, bit [0] will be 1 (because it was 0).
a= a|0x02; [Here, bit [1] will be 1 (because it was 0).
a= a|0x04; [Here, bit [2] will be 1 (because it was 0).
a= a|0x08; [Here, bit [3] will be 1 (because it was 0).
Thus, the final output will be '0F' (all four bits from 0 to 3 are set now).

Program - 5
#include <stdio.h>

int main()
{
unsigned char a=0x00;

a = ~a;

printf("a= %02X\n",a);

return 0;
}

Output
a= FF

Explanation

Bitwise NOT/ Negation/ One's compliment operator (~) reverses the bit(s) from 0 to 1 and 1 to 0,
in this program value of a is 0x00 (in Binary: 0000 0000), statement a = ~a will reverse all the
bits and assigned them into variable a again, then, the value of a will be 0xFF [in Binary: 1111
1111].

Program - 1
#include <stdio.h>
int main()
{
unsigned char a=0xFA;

a= (a>>4) | (a<<4);

printf("a = %02X\n",a);

return 0;
}

Output
a= AF

Explanation

The expression a= (a>>4) | (a<<4); will swap the nibbles, here a>>4 will shift the last 4 bits
(from 7 to 4) to the first 4 position (3 to 0) and statement a<<4 will shift the first 4 bits from (0 to
3), to the last 4 position (from 4 to 7). And the Bitwise OR ('|') operator will add both of the
nibbles, thus the output will be AF (swapped nibbles).

Program - 2
#include <stdio.h>
int main()
{
unsigned char a=0xFA;
char loop;

for(loop=7; loop>=0; loop--)


printf("%d ",(a & (1<<loop))?1:0);

printf("\n");

return 0;
}

Output
1 1 1 1 1 0 1 0
Explanation

This program is to get BINARY value of any 8 bits integer number, expression (a & (1<<loop))?
1:0 will check whether particular bit is set (high) or not, if it is set (high), '1' will print else '0'
will print.

Program - 3
#include <stdio.h>
int main()
{
unsigned char a=0xAA;
unsigned char b=0x55;

printf("(a^b): %02X\n",(a^b));

return 0;
}

Output
FF

Explanation

Bitwise XOR (^) returns 1, if one operand's bit is 1 and other operand's bit is 0, consider the
given truth table:

a b a^b
0 0 0
0 1 1
1 0 1
1 1 0

Expression (a^b)
a: 0xAA 1010 1010
b: 0x55 0101 0101
(a^b) 0xFF 1111 1111

Thus, the output will be "FF".

Program - 4
#include <stdio.h>
int main()
{
unsigned char a=0xAA;

a= (a^0x55);
a= (a^0x55);

printf("a= %02X\n",a);
return 0;
}

Output
a= AA

Explanation

When, some value is XORed with a number twice, same (initial value) returns.
Consider the expression evaluation, first (a= a^0x55)
a: 0xAA - 1010 1010
0x55 - 0101 0101
(a^0x55) - 1111 1111 = 0xFF

Now, second expression (a= a^0x55)


a: 0xFF - 1111 1111
0x55 - 0101 0101
(a^0x55) - 1010 1010 = 0x55
Thus, the output will be "AA".

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy