473,396 Members | 1,590 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

Nov 30 '06 #1
16 3693

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
Nov 30 '06 #2
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.

Nov 30 '06 #3
#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 ?"
Nov 30 '06 #4
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
Nov 30 '06 #5
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
Nov 30 '06 #6
"Santosh Nayak" <sa***********@gmail.comwrites:
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) ks***@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.
Nov 30 '06 #7
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.
Nov 30 '06 #8
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.
Nov 30 '06 #9
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>
Nov 30 '06 #10
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.

Nov 30 '06 #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 ;
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/...a68f5071ead837

--
pete
Nov 30 '06 #12
2006-11-30 <11**********************@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 >>
Nov 30 '06 #13
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.
<grinI 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.
Nov 30 '06 #14
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/...a68f5071ead837
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.
Nov 30 '06 #15
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.

<grinI 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. << << << ><< >>
Nov 30 '06 #16
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/...a68f5071ead837

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>
Nov 30 '06 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
by: Mehta Shailendrakumar | last post by:
Hello, Can anyone suggest me operator to perform arithmetic shift in C? May it be for a perticular compiler. Thank you in advance. Regards, Shailendra
17
by: orekinbck | last post by:
Hi There Say I want to check if object1.Property1 is equal to a value, but object1 could be null. At the moment I have code like this: if (object1 != null) { if (object1.Property ==...
2
by: Saber S | last post by:
When I press Alt+Shift in a text control, the cursor turns Right-To-Left. I want it without pressing Alt+Shift, I want to when I click a text control the cursor automatically turn to Right-To-Left.
11
by: Kenneth Lantrip | last post by:
Anyone got any ideas as to how this process could be improved for speed? this is what I have... Dim j, q As Integer Dim x(16), y(16) As Byte x.CopyTo(y, 0) ' shift left circular 24 bits
7
by: Marty McFly | last post by:
Hello VB Gurus, I have an unusual requirement to shift an unsigned int right one bit: Dim myVar As UInt32 = UInt32.Parse("123456") Dim myResult As UInt32 myResult = myVar >> 1 However, the...
4
by: sandhya | last post by:
Hello Folks, i hava a problem in coding of circular left shift of 25 bits in my program...how do i perform it, and how do i use unsigned in VB. My program (IDEA algorithm implementation in VB) ...
56
by: Christian Christmann | last post by:
Hi, in the header of my class I've a constant static const int a = ( 1 << 32 ) - 1; When compiling the code, g++ issues the warning "warning: left shift count >= width of type" Why? And...
1
by: marsguy85 | last post by:
a program that repeatedly request an integer from the user and displays the integer as a binary number. It should terminate when the value 9999 is entered. A typical run might look like: Enter...
4
by: G Iveco | last post by:
I am using this type of code to do right-shifting, B = 3; data1 = (data + (1 << (B-1))) >B; data1 seems incorrect when data = -4-8*i.. which means it rounds -1.5 to -1 instead of -2. On...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.