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
16 3719
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.
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
2006-11-30 <11************ **********@14g2 000cws.googlegr oups.com>,
Santosh Nayak wrote:
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.
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 >>
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.
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.
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. << << << ><< >>
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> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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)
|
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.
|
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
|
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.
| |
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!!!
|
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?
|
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 negative and check that they are correct – the storage of signed numbers in binary was discussed in the...
|
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.
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |