In: Computer Science
1. What is value of X after following operation
int X=0x 45;
X = X<<3 ; X=
What is value of X after following operation
int x=0x 40;
X = X>>3 ; X=
What is value of X after following operation
int x=0X FF;
X = X & 0x0F; X=
What is value of X after following operation
int x=0x FF;
X = ~X ; X=
What is value of X after following operation
int X=0x FF;
X = X &~(1<<4) ; X=
Literals that start with 0x
are hexadecimal
integers .
4 in binary = 0100
5 in binary = 0101
means 0x45 = 0100 0101
so int x =0x45 means in binary x will contain 0100 0101(with some preceding zeros according to compiler beacuse interger can take 32 or 64 bit according to compiler)
1.
int x= 0x45;
x=x<<3;
/*
given x =0000 0000 0000 0000 0000 0000 0100 0101
<< is left shift operator means we have to shift 3 bits towads left with inserting three extra zeros to right
takin 32 bit for integer
after applying bitwise left shift operator , shifting each bit to left by 3 bits
x= 0000 0000 0000 0000 0000 0010 0010 1000 = 552(in decimal number system)
*/
x= 552 // final answe
2.
int x= 0x40;
x=x>>3;
/*
x =0x40 means in binary x will contain 0100 0000(with some preceding zeros according to compiler beacuse interger can take 32 or 64 bit according to compiler)
given x =0000 0000 0000 0000 0000 0000 0100 0000
>> is right shift operator means we have to shift 3 bits towads right with inserting three extra zeros to left
takin 32 bit for integer
after applying bitwise right shift operator , shifting each bit to right by 3 bits
x= 0000 0000 0000 0000 0000 0000 0000 1000 = 8(in decimal number system)
*/
x= 8 // final answer
3.
int x=0X FF;
X = X & 0x0F;
/*
x =0xFF means in binary x will contain 1111 1111(with some preceding zeros according to compiler beacuse interger can take 32 or 64 bit according to compiler)
given x =0000 0000 0000 0000 0000 0000 1111 1111
& is bitwise AND operator, means we have to do bitwise anding (only 1&1=1, otherwise 0 for all cases)
x = 0000 0000 0000 0000 0000 0000 1111 1111
0x0F= 0000 0000 0000 0000 0000 0000 0000 1111 ( execute bitwise and for each bit)
X = 0000 0000 0000 0000 0000 0000 0000 1111
x= 0000 0000 0000 0000 0000 0000 0000 1111 = 15(in decimal number system)
*/
x=15 // final answer
4.
int x=0x FF;
X = ~X ;
/*
x =0xFF means in binary x will contain 1111 1111(with some preceding zeros according to compiler beacuse interger can take 32 or 64 bit according to compiler)
given x =0000 0000 0000 0000 0000 0000 1111 1111
x= 255 (in decimal)
~ is bitwise NOT operator, means we have to do bitwise negation of x;
simply in short ~x means -(x+1)
so here x= 255(in decimal)
after applying ~operator
x= -256;
*/
x= -256 // final answer
5.
X=0x FF;
X = X &~(1<<4) ;
/*
x =0xFF means in binary x will contain 1111 1111(with some preceding zeros according to compiler beacuse interger can take 32 or 64 bit according to compiler)
given x =0000 0000 0000 0000 0000 0000 1111 1111
1<<4 is 0000 0000 0000 0000 0000 0000 0001 0000 =(16 in decimal)
~ is bitwise NOT operator, means we have to do bitwise negation of x;
simply in short ~x means -(x+1)
so here ~(1<<4)= ~(16)= -17
x=x&~(1<<4); // apply bitwise and now
x=239;
*/
x= 239 // final answer