hi.......
i do have a doubt on this problem.please help me to solve it.here it goes........

main()
{
int x=5;
printf(“%d,%d,%d\n”,x,x<<2,>>2)
;
}
Answer: 5,20,1

now my problem is to know how the followin answer occurs?i know that its of rite shift and left shift which shifts the byte.can any one tell me clearly how tis answer comes?please....... thank you in advance.:?:

and how can we use a sizeof() in malloc operations?

Recommended Answers

All 2 Replies

You've pretty much answered your first question - the >> and << operators are shifting the bits of the initial value of x.

In binary, the byte that stores decimal 5 is 0000 0101. Shift that left two place, you get 0001 0100, which is 20. This new value is displayed, but the variable x is not actually modified.
So when you right shift, you again start with 5's value, and the resulting value displayed is 0000 0001. The left shift brings in zeros on the right end. The right shift dropped bits off the end.

As to sizeof when using malloc, how about

int *arr = (int * )malloc( 5 * sizeof( int ) );

malloc allocates in terms of bytes. You have to determine how many bytes will be needed for the particular data type you're using.

Val

thanks man.i got the answer 2 days before.but i want to thanks for ur attention and help.:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.