Velocity Reviews > Left Shift / Right Shift Operators

# Left Shift / Right Shift Operators

Santosh Nayak
Guest
Posts: n/a

 11-30-2006
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 ?"

Arthur J. O'Dwyer
Guest
Posts: n/a

 11-30-2006

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

Santosh Nayak
Guest
Posts: n/a

 11-30-2006
On Nov 30, 11:54 am, "Arthur J. O'Dwyer" <(E-Mail Removed)>
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.

onkar
Guest
Posts: n/a

 11-30-2006
#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 ?"

pete
Guest
Posts: n/a

 11-30-2006
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
Guest
Posts: n/a

 11-30-2006
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

Keith Thompson
Guest
Posts: n/a

 11-30-2006
"Santosh Nayak" <(E-Mail Removed)> writes:
> On Nov 30, 11:54 am, "Arthur J. O'Dwyer" <(E-Mail Removed)>
> 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.velocityreviews.com/forums/(E-Mail Removed) <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.

Richard Heathfield
Guest
Posts: n/a

 11-30-2006
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
Guest
Posts: n/a

 11-30-2006
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.

CBFalconer
Guest
Posts: n/a

 11-30-2006
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>