473,569 Members | 2,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3711

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...@andr ew.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...@andr ew.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_Keit h) 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

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

Similar topics

43
26459
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
3036
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 == desiredValue)
2
1628
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
4035
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
2903
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 >> operator only works on Byte, Short, Integer, and Long.
4
4262
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) requires unsigned bits...so how do i go thro this ,since VB does not support unsigned operations Post your suggestions!!!
56
15306
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 how can I get rid of that?
1
1683
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 integer: 16 00000000000000000000000000010000 Enter integer: -1 11111111111111111111111111111111 Enter integer: 9999 Try various values including...
4
4201
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 the positive side, 1.5 is rounded to 2, which ic correct.
1
7677
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7979
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1223
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
940
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.