f



Left Shift / Right Shift Operators

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

0
11/30/2006 6:49:31 AM
comp.lang.c 30657 articles. 5 followers. spinoza1111 (3246) is leader. Post Follow

16 Replies
515 Views

Similar Articles

[PageSpeed] 13

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
0
ajonospam (382)
11/30/2006 6:54:39 AM
On Nov 30, 11:54 am, "Arthur J. O'Dwyer" <ajonos...@andrew.cmu.edu>
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.

0
11/30/2006 7:08:08 AM
#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 ?"

0
onkar.n.m (70)
11/30/2006 7:19:14 AM
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
0
pfiland (6613)
11/30/2006 7:24:13 AM
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
0
pfiland (6613)
11/30/2006 7:34:50 AM
"Santosh Nayak" <santoshsnayak@gmail.com> writes:
> On Nov 30, 11:54 am, "Arthur J. O'Dwyer" <ajonos...@andrew.cmu.edu>
> 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) kst-u@mib.org  <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.
0
kst-u (21963)
11/30/2006 7:36:25 AM
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.
0
rjh (10791)
11/30/2006 8:04:37 AM
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.
0
rjh (10791)
11/30/2006 8:09:49 AM
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>


0
cbfalconer (19194)
11/30/2006 8:56:18 AM
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.

0
truedfx (1926)
11/30/2006 2:25:39 PM
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
0
pfiland (6613)
11/30/2006 2:50:44 PM
2006-11-30 <1164870487.967396.110880@14g2000cws.googlegroups.com>,
Santosh Nayak wrote:
> On Nov 30, 11:54 am, "Arthur J. O'Dwyer" <ajonos...@andrew.cmu.edu>
> 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 >>
0
random21 (344)
11/30/2006 3:30:06 PM
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.
0
rjh (10791)
11/30/2006 3:38:40 PM
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.
0
rjh (10791)
11/30/2006 3:46:28 PM
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.  << << << >> << >> >> >>

0
truedfx (1926)
11/30/2006 4:44:50 PM
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>


0
cbfalconer (19194)
11/30/2006 5:10:43 PM
Reply: