Left Shift / Right Shift Operators

Discussion in 'C Programming' started by Santosh Nayak, Nov 30, 2006.

  1. Hi,
    Is there any way to catch the losing bit occurring due to Right Shift
    Operator ?

    e.g
    int a = 5 ;
    a = a >> 1 ; // // a is now 2 and the least significant bit is lost //
    //

    I want this solution for the question:
    "How to find if the number is even or odd using only "<<" or/and ">>"
    operators ?"
     
    Santosh Nayak, Nov 30, 2006
    #1
    1. Advertising

  2. On Thu, 29 Nov 2006, Santosh Nayak wrote:
    >
    > Is there any way to catch the losing bit occurring due to Right Shift
    > Operator ?


    int a = 5;
    int lost_bit = a & 1;
    a = a >> 1;
    /* now lost_bit holds the lost bit */

    > I want this solution for the question:
    > "How to find if the number is even or odd using only "<<" or/and ">>"
    > operators ?"


    We won't do your homework for you. Keep thinking.

    -Arthur
     
    Arthur J. O'Dwyer, Nov 30, 2006
    #2
    1. Advertising

  3. On Nov 30, 11:54 am, "Arthur J. O'Dwyer" <>
    wrote:
    > int a = 5;
    > int lost_bit = a & 1;


    Perhaps, i was not clear about my question.
    I meant we are not supposed to use any other bitwise operators other
    than "<<" or ">>".
    "&" operator is not allowed.
     
    Santosh Nayak, Nov 30, 2006
    #3
  4. Santosh Nayak

    onkar Guest

    #include<stdio.h>
    #include<stdlib.h>
    int main(int argc,char **argv){
    int i=atoi(argv[1]);
    if(i&1)
    printf("odd\n");
    else{
    if(i==0)
    printf("invalid\n");
    else
    printf("even\n");
    }
    return 0;
    }

    will work for u // pass number from command line

    regards,
    Onkar


    Santosh Nayak wrote:
    > Hi,
    > Is there any way to catch the losing bit occurring due to Right Shift
    > Operator ?
    >
    > e.g
    > int a = 5 ;
    > a = a >> 1 ; // // a is now 2 and the least significant bit is lost //
    > //
    >
    > I want this solution for the question:
    > "How to find if the number is even or odd using only "<<" or/and ">>"
    > operators ?"
     
    onkar, Nov 30, 2006
    #4
  5. Santosh Nayak

    pete Guest

    Santosh Nayak wrote:
    >
    > Hi,
    > Is there any way to catch the losing bit occurring due to Right Shift
    > Operator ?
    >
    > e.g
    > int a = 5 ;
    > a = a >> 1 ; // // a is now 2 and the least significant bit is lost //
    > //
    >
    > I want this solution for the question:
    > "How to find if the number is even or odd using only "<<" or/and ">>"
    > operators ?"


    b = 0 > a ? -a : a;
    printf("%d is %s.\n", a, b - (b >> 1 << 1) ? "odd" : "even");

    --
    pete
     
    pete, Nov 30, 2006
    #5
  6. Santosh Nayak

    pete Guest

    pete wrote:
    >
    > Santosh Nayak wrote:
    > >
    > > Hi,
    > > Is there any way to catch the losing bit occurring due to Right Shift
    > > Operator ?
    > >
    > > e.g
    > > int a = 5 ;
    > > a = a >> 1 ; // // a is now 2 and the least significant bit is lost //
    > > //
    > >
    > > I want this solution for the question:
    > > "How to find if the number is even or odd
    > > using only "<<" or/and ">>" operators ?"

    >
    > b = 0 > a ? -a : a;
    > printf("%d is %s.\n", a, b - (b >> 1 << 1) ? "odd" : "even");



    if (-INT_MAX > a) {
    printf("%d is even.\n", a);
    } else {
    b = 0 > a ? -a : a;
    printf("%d is %s.\n", a, b - (b >> 1 << 1) ? "odd" : "even");
    }

    --
    pete
     
    pete, Nov 30, 2006
    #6
  7. "Santosh Nayak" <> writes:
    > On Nov 30, 11:54 am, "Arthur J. O'Dwyer" <>
    > wrote:
    >> int a = 5;
    >> int lost_bit = a & 1;

    >
    > Perhaps, i was not clear about my question.
    > I meant we are not supposed to use any other bitwise operators other
    > than "<<" or ">>".
    > "&" operator is not allowed.


    As Arthur wrote:

    | We won't do your homework for you. Keep thinking.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Nov 30, 2006
    #7
  8. onkar said:

    > #include<stdio.h>
    > #include<stdlib.h>
    > int main(int argc,char **argv){
    > int i=atoi(argv[1]);
    > if(i&1)
    > printf("odd\n");
    > else{
    > if(i==0)
    > printf("invalid\n");
    > else
    > printf("even\n");
    > }
    > return 0;
    > }
    >
    > will work for u


    Possibly, whoever 'u' is - but it won't work for Santosh Nayak, since it
    overlooks a rather important constraint which he clearly mentioned in his
    original article.

    Let us hope that 'u' is never so careless as to type the wrong thing on the
    command line, or to omit it completely, because your program fails to deal
    with these possibilities, and will exhibit undefined behaviour if they
    occur.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
     
    Richard Heathfield, Nov 30, 2006
    #8
  9. pete said:

    > Santosh Nayak wrote:
    >>
    >> Hi,
    >> Is there any way to catch the losing bit occurring due to Right Shift
    >> Operator ?
    >>
    >> e.g
    >> int a = 5 ;
    >> a = a >> 1 ; // // a is now 2 and the least significant bit is lost //
    >> //
    >>
    >> I want this solution for the question:
    >> "How to find if the number is even or odd using only "<<" or/and ">>"
    >> operators ?"

    >
    > b = 0 > a ? -a : a;
    > printf("%d is %s.\n", a, b - (b >> 1 << 1) ? "odd" : "even");


    Your use of these operators:

    = > ?: -

    violates the OP's constraint.

    Here is a more accurate (although admittedly not terribly informative) way
    to find if the number is even or odd:

    int a = 5;
    puts("Yes.");
    /* oops, I forgot to use the << and >> operators, so I'll do that now */
    a >> 1;
    a << 1;

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
     
    Richard Heathfield, Nov 30, 2006
    #9
  10. Santosh Nayak

    CBFalconer Guest

    Santosh Nayak wrote:
    >
    > Is there any way to catch the losing bit occurring due to Right
    > Shift Operator ?


    The answer is yes.

    .... snip ...
    >
    > I want this solution for the question:
    > "How to find if the number is even or odd using only "<<" or/and
    > ">>" operators ?"


    Try thinking.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>
     
    CBFalconer, Nov 30, 2006
    #10
  11. Richard Heathfield wrote:
    > pete said:
    >
    > > Santosh Nayak wrote:
    > >>
    > >> Hi,
    > >> Is there any way to catch the losing bit occurring due to Right Shift
    > >> Operator ?
    > >>
    > >> e.g
    > >> int a = 5 ;
    > >> a = a >> 1 ; // // a is now 2 and the least significant bit is lost //
    > >> //
    > >>
    > >> I want this solution for the question:
    > >> "How to find if the number is even or odd using only "<<" or/and ">>"
    > >> operators ?"

    > >
    > > b = 0 > a ? -a : a;
    > > printf("%d is %s.\n", a, b - (b >> 1 << 1) ? "odd" : "even");

    >
    > Your use of these operators:
    >
    > = > ?: -
    >
    > violates the OP's constraint.
    >
    > Here is a more accurate (although admittedly not terribly informative) way
    > to find if the number is even or odd:
    >
    > int a = 5;
    > puts("Yes.");
    > /* oops, I forgot to use the << and >> operators, so I'll do that now */
    > a >> 1;
    > a << 1;


    Your use of the function call operator violates the OP's constraint.

    Here's a more accurate /and/ informative, but not entirely portable,
    way to find if the number is even or odd:

    int main(int argc, char *argv[]) {
    struct { unsigned b : 1; } s = { argc };
    return s.b;
    }

    Pointless use of << and >> can be added here too.
     
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=, Nov 30, 2006
    #11
  12. Santosh Nayak

    pete Guest

    Richard Heathfield wrote:
    >
    > pete said:
    >
    > > Santosh Nayak wrote:
    > >>
    > >> Hi,
    > >> Is there any way to catch the losing bit
    > >> occurring due to Right Shift Operator ?
    > >>
    > >> e.g
    > >> int a = 5 ;
    > >> a = a >> 1 ;


    > >> I want this solution for the question:
    > >> "How to find if the number is even or odd using
    > >> only "<<" or/and ">>"
    > >> operators ?"

    > >
    > > b = 0 > a ? -a : a;
    > > printf("%d is %s.\n", a, b - (b >> 1 << 1) ? "odd" : "even");

    >
    > Your use of these operators:
    >
    > = > ?: -
    >
    > violates the OP's constraint.


    OP restated his constraint in another strand of this thread,
    as only applying to bitwise operators:

    "Perhaps, i was not clear about my question.
    I meant we are not supposed to use any other bitwise operators
    other than "<<" or ">>"."

    http://groups-beta.google.com/group/comp.lang.c/msg/9ea68f5071ead837

    --
    pete
     
    pete, Nov 30, 2006
    #12
  13. Santosh Nayak

    Random832 Guest

    2006-11-30 <>,
    Santosh Nayak wrote:
    > On Nov 30, 11:54 am, "Arthur J. O'Dwyer" <>
    > wrote:
    >> int a = 5;
    >> int lost_bit = a & 1;

    >
    > Perhaps, i was not clear about my question.
    > I meant we are not supposed to use any other bitwise operators other
    > than "<<" or ">>".
    > "&" operator is not allowed.


    You don't need to catch the shifted-off bit to detect if a number is odd
    or even, but in order to avoid doing so you need to use _both_ << and >>
     
    Random832, Nov 30, 2006
    #13
  14. Harald van D?k said:

    > Richard Heathfield wrote:
    >> pete said:
    >>
    >> > Santosh Nayak wrote:
    >> >>
    >> >> Hi,
    >> >> Is there any way to catch the losing bit occurring due to Right Shift
    >> >> Operator ?
    >> >>
    >> >> e.g
    >> >> int a = 5 ;
    >> >> a = a >> 1 ; // // a is now 2 and the least significant bit is lost //
    >> >> //
    >> >>
    >> >> I want this solution for the question:
    >> >> "How to find if the number is even or odd using only "<<" or/and ">>"
    >> >> operators ?"
    >> >
    >> > b = 0 > a ? -a : a;
    >> > printf("%d is %s.\n", a, b - (b >> 1 << 1) ? "odd" : "even");

    >>
    >> Your use of these operators:
    >>
    >> = > ?: -
    >>
    >> violates the OP's constraint.
    >>
    >> Here is a more accurate (although admittedly not terribly informative)
    >> way to find if the number is even or odd:
    >>
    >> int a = 5;
    >> puts("Yes.");
    >> /* oops, I forgot to use the << and >> operators, so I'll do that now */
    >> a >> 1;
    >> a << 1;

    >
    > Your use of the function call operator violates the OP's constraint.


    <grin> I wondered if anyone would pick that up. I don't think the OP would
    be too concerned about an output call, though. Okay, perhaps not /that/
    output!

    > Here's a more accurate /and/ informative, but not entirely portable,
    > way to find if the number is even or odd:
    >
    > int main(int argc, char *argv[]) {
    > struct { unsigned b : 1; } s = { argc };
    > return s.b;
    > }


    Use of the structure member operator violates the OP's constraint. :)

    > Pointless use of << and >> can be added here too.


    Thanks. << << << >> << >> >> >>

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
     
    Richard Heathfield, Nov 30, 2006
    #14
  15. pete said:

    > Richard Heathfield wrote:
    >>
    >> pete said:
    >>

    <snip>
    >> >
    >> > b = 0 > a ? -a : a;
    >> > printf("%d is %s.\n", a, b - (b >> 1 << 1) ? "odd" : "even");

    >>
    >> Your use of these operators:
    >>
    >> = > ?: -
    >>
    >> violates the OP's constraint.

    >
    > OP restated his constraint in another strand of this thread,
    > as only applying to bitwise operators:
    >
    > "Perhaps, i was not clear about my question.
    > I meant we are not supposed to use any other bitwise operators
    > other than "<<" or ">>"."
    >
    > http://groups-beta.google.com/group/comp.lang.c/msg/9ea68f5071ead837


    In which case it's trivial.

    int is_odd(unsigned long foo)
    {
    int i = (2 << 1) >> 1;

    return foo % i;
    }

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
     
    Richard Heathfield, Nov 30, 2006
    #15
  16. Richard Heathfield wrote:
    > Harald van D?k said:
    >
    > > Richard Heathfield wrote:
    > >> pete said:
    > >>
    > >> > Santosh Nayak wrote:
    > >> >>
    > >> >> Hi,
    > >> >> Is there any way to catch the losing bit occurring due to Right Shift
    > >> >> Operator ?
    > >> >>
    > >> >> e.g
    > >> >> int a = 5 ;
    > >> >> a = a >> 1 ; // // a is now 2 and the least significant bit is lost //
    > >> >> //
    > >> >>
    > >> >> I want this solution for the question:
    > >> >> "How to find if the number is even or odd using only "<<" or/and ">>"
    > >> >> operators ?"
    > >> >
    > >> > b = 0 > a ? -a : a;
    > >> > printf("%d is %s.\n", a, b - (b >> 1 << 1) ? "odd" : "even");
    > >>
    > >> Your use of these operators:
    > >>
    > >> = > ?: -
    > >>
    > >> violates the OP's constraint.
    > >>
    > >> Here is a more accurate (although admittedly not terribly informative)
    > >> way to find if the number is even or odd:
    > >>
    > >> int a = 5;
    > >> puts("Yes.");
    > >> /* oops, I forgot to use the << and >> operators, so I'll do that now */
    > >> a >> 1;
    > >> a << 1;

    > >
    > > Your use of the function call operator violates the OP's constraint.

    >
    > <grin> I wondered if anyone would pick that up. I don't think the OP would
    > be too concerned about an output call, though. Okay, perhaps not /that/
    > output!
    >
    > > Here's a more accurate /and/ informative, but not entirely portable,
    > > way to find if the number is even or odd:
    > >
    > > int main(int argc, char *argv[]) {
    > > struct { unsigned b : 1; } s = { argc };
    > > return s.b;
    > > }

    >
    > Use of the structure member operator violates the OP's constraint. :)


    Good point. :)

    #include <tgmath.h>
    int main(int argc, char *argv[]) {
    _Bool b = fmod(argc, 2); /* macro invocation, not a function call
    */
    return b;
    }

    (The _Bool variable is because otherwise, rounding errors cause
    problems on my system.)

    > > Pointless use of << and >> can be added here too.

    >
    > Thanks. << << << >> << >> >> >>
     
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=, Nov 30, 2006
    #16
  17. Santosh Nayak

    CBFalconer Guest

    Richard Heathfield wrote:
    > pete said:
    >> Richard Heathfield wrote:
    >>> pete said:
    >>>

    > <snip>
    >>>>
    >>>> b = 0 > a ? -a : a;
    >>>> printf("%d is %s.\n", a, b - (b >> 1 << 1) ? "odd" : "even");
    >>>
    >>> Your use of these operators:
    >>>
    >>> = > ?: -
    >>>
    >>> violates the OP's constraint.

    >>
    >> OP restated his constraint in another strand of this thread,
    >> as only applying to bitwise operators:
    >>
    >> "Perhaps, i was not clear about my question.
    >> I meant we are not supposed to use any other bitwise operators
    >> other than "<<" or ">>"."
    >>
    >> http://groups-beta.google.com/group/comp.lang.c/msg/9ea68f5071ead837

    >
    > In which case it's trivial.
    >
    > int is_odd(unsigned long foo)
    > {
    > int i = (2 << 1) >> 1;
    >
    > return foo % i;
    > }


    Now that you have started doing his homework for him, I suggest:

    int is_odd(unsigned long foo) {

    return foo - ((foo >> 1) << 1);
    }

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>
     
    CBFalconer, Nov 30, 2006
    #17
    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. Wenjie
    Replies:
    3
    Views:
    1,039
    Ron Samuel Klatchko
    Jul 11, 2003
  2. G Iveco
    Replies:
    4
    Views:
    458
    James Kanze
    Jul 31, 2007
  3. Xoomer
    Replies:
    1
    Views:
    298
    Ian Collins
    Aug 23, 2007
  4. Sanny
    Replies:
    38
    Views:
    3,422
    Thomas Richter
    Apr 29, 2011
  5. pc
    Replies:
    2
    Views:
    1,321
    crisgoogle
    Jun 8, 2011
Loading...

Share This Page