Test you C skills !!!!!!!

Discussion in 'C Programming' started by aarklon@gmail.com, Nov 14, 2007.

  1. Guest

    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...???

    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...???
    , Nov 14, 2007
    #1
    1. Advertising

  2. said:

    > 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'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.

    > 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...???


    The Standard doesn't say. It is entirely up to the implementation, and it
    is certainly true that some implementations do this in the described
    manner.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Nov 14, 2007
    #2
    1. Advertising

  3. Guest

    On Nov 14, 4:53 pm, wrote:
    > 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()...???


    To free() you can pass the return value of malloc(), realloc() and
    calloc().
    May that be a unique pointer, a valid pointer or NULL.
    , Nov 14, 2007
    #3
  4. 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...???


    The book is correct, your friends are not.

    n1256 6.5.7p4 says:
    "The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated
    bits are filled with zeros."
    p5 says:
    "The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has
    an unsigned type or if E1 has a signed type and a nonnegative value, the
    value of the result is the integral part of the quotient of E1 / 2^{E2}.
    If E1 has a signed type and a negative value, the resulting value is
    implementation-defined."

    (Where x^{y} is x to the power of y)

    In theory, the implementation could define right-shifting of negative
    numbers to be a rotate-right operation instead. But it couldn't do the
    same of positive numbers, so the principle of least surprise demands
    that an implementation shift in zeroes or sign-extend (logical or
    arithmetic right shift respectively).

    This leads me to an interesting question: Is right-shifting defined at
    all on plain char?

    --
    Philip Potter pgp <at> doc.ic.ac.uk
    Philip Potter, Nov 14, 2007
    #4
  5. Mark Bluemel Guest

    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...???


    Did you look to find out what the standard says?
    Or a standard reference text?
    If so, what didn't you understand about the answer?
    If not, why not? Do you trust uninformed chatter with your friends
    more than the standard or a good text book?

    > 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...???


    All that is required is that free knows how many bytes to deallocate,
    the way it knows that is up to the implementer. They may use the
    strategy you describe, but there are others. A C programmer has no
    need, generally, to know what strategy is used.
    Mark Bluemel, Nov 14, 2007
    #5
  6. Philip Potter <> writes:

    > This leads me to an interesting question: Is right-shifting defined at
    > all on plain char?


    The operands are promoted so in one sense the answer is no (because
    the shift happens on the promoted value) and in another sense yes
    because the meaning will depend on what char promotes to.

    --
    Ben.
    Ben Bacarisse, Nov 14, 2007
    #6
  7. Ben Pfaff Guest

    writes:

    > On Nov 14, 4:53 pm, wrote:
    >> 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()...???

    >
    > To free() you can pass the return value of malloc(), realloc() and
    > calloc().
    > May that be a unique pointer, a valid pointer or NULL.


    This is all true, but it's hard to see how it bears on the OP's
    question.
    --
    Ben Pfaff
    http://benpfaff.org
    Ben Pfaff, Nov 14, 2007
    #7
  8. pete Guest

    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...???


    Your friends don't know what they're talking about.

    > 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...???


    I don't care.
    Either you know what your target platform is, or you don't.

    If you don't,
    then it doesn't make sense to write code as though you do.

    --
    pete
    pete, Nov 14, 2007
    #8
  9. Old Wolf Guest

    On Nov 15, 4:26 am, Richard Heathfield <> wrote:
    > 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. 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).
    Old Wolf, Nov 14, 2007
    #9
  10. Jack Klein Guest

    On 14 Nov 2007 14:02:28 -0800, Old Wolf <> wrote
    in comp.lang.c:

    > On Nov 15, 4:26 am, Richard Heathfield <> wrote:
    > > 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. 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).


    Not on unsigned types, it couldn't.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Nov 15, 2007
    #10
  11. Old Wolf said:

    > On Nov 15, 4:26 am, Richard Heathfield <> wrote:
    >> 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@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Nov 15, 2007
    #11
  12. 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
    Philip Potter, Nov 15, 2007
    #12
  13. 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@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Nov 15, 2007
    #13
  14. 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.

    (What are you talking about?)

    --
    Philip Potter pgp <at> doc.ic.ac.uk
    Philip Potter, Nov 15, 2007
    #14
  15. On Nov 14, 7:53 pm, 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
    karthikbalaguru, Nov 15, 2007
    #15
  16. Mark Bluemel Guest

    karthikbalaguru wrote:
    > On Nov 14, 7:53 pm, 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?
    Mark Bluemel, Nov 15, 2007
    #16
  17. karthikbalaguru <> writes:

    > On Nov 14, 7:53 pm, 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.
    Ben Bacarisse, Nov 15, 2007
    #17
  18. pete Guest

    Richard Heathfield wrote:
    >
    > Old Wolf said:
    >
    > > On Nov 15, 4:26 am, Richard Heathfield <> wrote:
    > >> 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
    pete, Nov 15, 2007
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Turner
    Replies:
    0
    Views:
    288
    Andy Turner
    May 19, 2004
  2. Sharath

    Test Your C Skills - Errors

    Sharath, Apr 19, 2007, in forum: C Programming
    Replies:
    24
    Views:
    804
    Christopher Benson-Manica
    Apr 23, 2007
  3. pmp
    Replies:
    1
    Views:
    6,985
    .rhavin grobert
    Oct 24, 2008
  4. Skybuck Flying

    Call oddities: &Test() vs &Test vs Test

    Skybuck Flying, Oct 4, 2009, in forum: C Programming
    Replies:
    1
    Views:
    695
    Skybuck Flying
    Oct 4, 2009
  5. pmp
    Replies:
    7
    Views:
    2,629
    Phil Carmody
    Oct 28, 2009
Loading...

Share This Page