Left Shift / Right Shift Operators

S

Santosh Nayak

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

Arthur J. O'Dwyer

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
 
S

Santosh Nayak

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

onkar

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

pete

Santosh said:
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");
 
P

pete

pete said:
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");
}
 
K

Keith Thompson

Santosh Nayak said:
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.
 
R

Richard Heathfield

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

Richard Heathfield

pete said:
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;
 
C

CBFalconer

Santosh said:
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.
 
G

Guest

Richard said:
pete said:


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

Random832

2006-11-30 said:
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 >>
 
R

Richard Heathfield

Harald van D?k said:
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. << << << >> << >> >> >>
 
G

Guest

Richard said:
Harald van D?k said:
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.)
 
C

CBFalconer

Richard said:
pete said:

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top