Velocity Reviews > Test you C skills !!!!!!!

Test you C skills !!!!!!!

Richard Heathfield
Guest
Posts: n/a

 11-15-2007
Old Wolf said:

> On Nov 15, 4:26 am, Richard Heathfield <(E-Mail Removed)> wrote:
>> (E-Mail Removed) said:
>>
>> > 1)On shifting, the bits from the left are rotated and brought to the
>> > right and accomodated where there is empty space on the right.

>>
>> > the reader is asked to tell if this statement is true or not???

>>
>> It's false for one and a half reasons. Firstly, no rotation occurs.
>> Secondly, even if it did, the statement as given would apply only to
>> left shifts.

>
> I assume he is talking about left shifts.

Okay.

> A left shift
> could be implemented as a rotation (shifting a 0 off the
> left requires a 0 to enter at the right; shifting a 1
> off the left causes undefined behaviour).

Shifting a 1 off the left does not necessarily cause undefined behaviour.
The behaviour for unsigned types, and for signed types with non-negative
values, is defined in 6.5.7(4):

"The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits
are filled with zeros. If E1 has an unsigned type, the value of the result
is E1 x 2 [to the power of] E2, reduced modulo one more than the maximum
value representable in the result type. If E1 has a signed type and
nonnegative value, and E1 x 2 [to the power of] E2 is representable in the
result type, then that is the resulting value; otherwise, the behavior is
undefined."

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999

Philip Potter
Guest
Posts: n/a

 11-15-2007
Richard Heathfield wrote:
> Old Wolf said:
>> A left shift
>> could be implemented as a rotation (shifting a 0 off the
>> left requires a 0 to enter at the right; shifting a 1
>> off the left causes undefined behaviour).

>
> Shifting a 1 off the left does not necessarily cause undefined behaviour.
> The behaviour for unsigned types, and for signed types with non-negative
> values, is defined in 6.5.7(4):
>
> "The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits
> are filled with zeros. If E1 has an unsigned type, the value of the result
> is E1 x 2 [to the power of] E2, reduced modulo one more than the maximum
> value representable in the result type. If E1 has a signed type and
> nonnegative value, and E1 x 2 [to the power of] E2 is representable in the
> result type, then that is the resulting value; otherwise, the behavior is
> undefined."

But how could you ever shift a one out of the left of a non-negative
integer? My reading of n1256 6.2.6.2p2 is that since the value bits of a
signed type are defined in terms of the corresponding value bits of an
unsigned type, they must be the N rightmost bits, and therefore the sign
bit must be the leftmost (non-padding) bit. Therefore a non-negative
integer must necessarily have leftmost bit zero.

Shifting a 1 off the left of a signed type therefore implies
left-shifting a negative number, and undefined behaviour.

Of course shifting a 1 off the left of an unsigned type is perfectly
well-defined.

--
Philip Potter pgp <at> doc.ic.ac.uk

Richard Heathfield
Guest
Posts: n/a

 11-15-2007
Philip Potter said:

<snip>

> But how could you ever shift a one out of the left of a non-negative
> integer?

Oh yeah, you'd probably need a paint spray, wouldn't you? And you'd have to
wait until the processor's back was turned...

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999

Philip Potter
Guest
Posts: n/a

 11-15-2007
Richard Heathfield wrote:
> Philip Potter said:
>
> <snip>
>
>> But how could you ever shift a one out of the left of a non-negative
>> integer?

>
> Oh yeah, you'd probably need a paint spray, wouldn't you? And you'd have to
> wait until the processor's back was turned...

That strikes me as highly shifty behaviour.

--
Philip Potter pgp <at> doc.ic.ac.uk

karthikbalaguru
Guest
Posts: n/a

 11-15-2007
On Nov 14, 7:53 pm, (E-Mail Removed) wrote:
> Hi all,
>
> I was going through the book Test your C skills by "Test your C
> skills" by Yashavant p. Kanetkar,ISBN 81-7029-801-6
>
> I have got the following doubts ,questions...???
>
> 1)On shifting, the bits from the left are rotated and brought to the
> right and accomodated where there is empty space on the right.
>
> the reader is asked to tell if this statement is true or not???
>
> The answer is given as false, when i discussed with my friends they
> told me that correct answer is implementation dependent.
>
> well what is the correct answer...???

It is correct for left shifts.
Signed Number bit shifting and Unsigned Number bit shifting should
be handled with card.

>
> 2) To free() we only pass the pointer to the block of memory which we
> want to deallocate.Then how does free() know how many bytes it should
> deallocate()...???
>
> the answer is given as follows:-
>
> In most implementations of malloc() the number of bytes allocated
> is stored adjacent to the allocated block.Hence it is simple for
> free() to know how many bytes to deallocate().
>
> To what extent this answer is true...???

Implementation dependent.

Karthik Balaguru

Mark Bluemel
Guest
Posts: n/a

 11-15-2007
karthikbalaguru wrote:
> On Nov 14, 7:53 pm, (E-Mail Removed) wrote:
>> Hi all,
>>
>> I was going through the book Test your C skills by "Test your C
>> skills" by Yashavant p. Kanetkar,ISBN 81-7029-801-6
>>
>> I have got the following doubts ,questions...???
>>
>> 1)On shifting, the bits from the left are rotated and brought to the
>> right and accomodated where there is empty space on the right.
>>
>> the reader is asked to tell if this statement is true or not???
>>
>> The answer is given as false, when i discussed with my friends they
>> told me that correct answer is implementation dependent.
>>
>> well what is the correct answer...???

>
> It is correct for left shifts.

Nope. Given that several people have given the right answer, why add a
wrong one?

> Signed Number bit shifting and Unsigned Number bit shifting should
> be handled with card.

What on earth do you mean by that?

Ben Bacarisse
Guest
Posts: n/a

 11-15-2007
karthikbalaguru <(E-Mail Removed)> writes:

> On Nov 14, 7:53 pm, (E-Mail Removed) wrote:
>> Hi all,
>>
>> I was going through the book Test your C skills by "Test your C
>> skills" by Yashavant p. Kanetkar,ISBN 81-7029-801-6
>>
>> I have got the following doubts ,questions...???
>>
>> 1)On shifting, the bits from the left are rotated and brought to the
>> right and accomodated where there is empty space on the right.
>>
>> the reader is asked to tell if this statement is true or not???
>>
>> The answer is given as false, when i discussed with my friends they
>> told me that correct answer is implementation dependent.
>>
>> well what is the correct answer...???

>
> It is correct for left shifts.

What is the "it"? The OP gives two answers (both wrong) but which one
are saying is correct for left shifts? Shifts don't rotate and left
shifts are either undefined or well-defined arithmetically -- never
implementation defined.

--
Ben.

pete
Guest
Posts: n/a

 11-15-2007
Richard Heathfield wrote:
>
> Old Wolf said:
>
> > On Nov 15, 4:26 am, Richard Heathfield <(E-Mail Removed)> wrote:
> >> (E-Mail Removed) said:
> >>
> >> > 1)On shifting,
> >> > the bits from the left are rotated and brought to the
> >> > right and accomodated where there is empty space on the right.
> >>
> >> > the reader is asked to tell if this statement is true or not???
> >>
> >> It's false for one and a half reasons. Firstly, no rotation occurs.
> >> Secondly, even if it did,
> >> the statement as given would apply only to
> >> left shifts.

> >
> > I assume he is talking about left shifts.

>
> Okay.
>
> > A left shift
> > could be implemented as a rotation (shifting a 0 off the
> > left requires a 0 to enter at the right; shifting a 1
> > off the left causes undefined behaviour).

>
> Shifting a 1 off the left does not necessarily
> cause undefined behaviour.
> The behaviour for unsigned types,
> and for signed types with non-negative
> values, is defined in 6.5.7(4):
>
> "The result of E1 << E2 is E1 left-shifted E2 bit positions;
> vacated bits are filled with zeros.
> If E1 has an unsigned type, the value of the result
> is E1 x 2 [to the power of] E2,
> reduced modulo one more than the maximum
> value representable in the result type.
> If E1 has a signed type and
> nonnegative value, and E1 x 2 [to the power of]
> E2 is representable in the result type,
> then that is the resulting value; otherwise, the behavior is
> undefined."

C89 doesn't say what happens if E1 is a signed type
for a left shift..

ISO/IEC 9899: 1990
6.3.7 Bitwise shift operators

The result of E1 << E2 is E1 left-shifted E2 bit positions;
vacated bits are filled with zeros.
If E1 has an unsigned type,
the value of the result is E1 multiplied by the quantity,
2 raised to the power E2, reduced modulo ULONG_MAX+1
if E1 has type unsigned long, UINT_MAX+1 otherwise.
(The constants ULONG_MAX and UINT_MAX
are defined in the header <limits.h>.)

The result of E1 >> E2 is E1 right-shifted E2 bit positions.

--
pete