# Left Shift / Right Shift Operators

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

1. ### Santosh NayakGuest

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

2. ### Arthur J. O'DwyerGuest

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

3. ### Santosh NayakGuest

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
4. ### onkarGuest

#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
5. ### peteGuest

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
6. ### peteGuest

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
7. ### Keith ThompsonGuest

"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
8. ### Richard HeathfieldGuest

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
9. ### Richard HeathfieldGuest

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");

= > ?: -

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
10. ### CBFalconerGuest

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

.... 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
11. ### =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=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 ; // // 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
12. ### peteGuest

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

--
pete

pete, Nov 30, 2006
13. ### Random832Guest

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
14. ### Richard HeathfieldGuest

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
15. ### Richard HeathfieldGuest

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

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
16. ### =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=Guest

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
17. ### CBFalconerGuest

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

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