473,406 Members | 2,769 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,406 software developers and data experts.

what is & 0x1 in this context?

I came across this nugget in my intarweb travels. It's part of a
median-calculation. Can someone tell me what the if( length & 0x1 ) is
doing?

Thanks!

---------------------------------------------
/* Is vector length odd or even ? */
if (length & 0x1)
ans = pCopy[length/2];
else
ans = .5 * (pCopy[length/2 - 1] + pCopy[length/2]);
Nov 14 '05 #1
5 19717
John wrote:
I came across this nugget in my intarweb travels. It's part of a
median-calculation. Can someone tell me what the if( length & 0x1 ) is
doing?

Thanks!

---------------------------------------------
/* Is vector length odd or even ? */
if (length & 0x1)
ans = pCopy[length/2];
else
ans = .5 * (pCopy[length/2 - 1] + pCopy[length/2]);


It's doing a bitwise AND on the least-significant bit of
"length". If it returns true (ie, that bit is set), then the
number is odd. Otherwise, it's even.

For example:

(5) 0101 & 0001 == 1 (odd, true)
(6) 0110 & 0001 == 0 (even, false)

--
Eric Enright /"\
ericAtiptsoftDcom \ / ASCII Ribbon Campaign
X Against HTML E-Mail
Public Key: 0xBEDF636F / \
Nov 14 '05 #2
agree

"John" <ia******@hotmail.com> дÈëÓʼþ
news:wu********************@comcast.com...
I came across this nugget in my intarweb travels. It's part of a
median-calculation. Can someone tell me what the if( length & 0x1 ) is
doing?

Thanks!

---------------------------------------------
/* Is vector length odd or even ? */
if (length & 0x1)
ans = pCopy[length/2];
else
ans = .5 * (pCopy[length/2 - 1] + pCopy[length/2]);

Nov 14 '05 #3
John <ia******@hotmail.com> wrote:
I came across this nugget in my intarweb travels. It's part of a
median-calculation. Can someone tell me what the if( length & 0x1 ) is
doing?

Thanks!

---------------------------------------------
/* Is vector length odd or even ? */
if (length & 0x1)
ans = pCopy[length/2];
else
ans = .5 * (pCopy[length/2 - 1] + pCopy[length/2]);


Well it's the same as (lenght & 1) which will be true when
the least significant bit of the value of length is set. That
means the value in length is odd.

Did you read the comment above the code?
--
Z (Zo**********@daimlerchrysler.com)
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond
Nov 14 '05 #4
"John" <ia******@hotmail.com> wrote in message news:<wu********************@comcast.com>...
I came across this nugget in my intarweb travels. It's part of a
median-calculation. Can someone tell me what the if( length & 0x1 ) is
doing?

Thanks!

---------------------------------------------
/* Is vector length odd or even ? */
if (length & 0x1)
ans = pCopy[length/2];
else
ans = .5 * (pCopy[length/2 - 1] + pCopy[length/2]);


"length & 0x1" get the left most bit of length so it is equivalent to
"length % 2"
Nov 14 '05 #5
Yep, i did read the comment. And after hearing it, it does make sense. I
guess I was having a senior moment =)
"Zoran Cutura" <zo**********@daimlerchrysler.com> wrote in message
news:ca**********@news.sns-felb.debis.de...
John <ia******@hotmail.com> wrote:
I came across this nugget in my intarweb travels. It's part of a
median-calculation. Can someone tell me what the if( length & 0x1 ) is
doing?

Thanks!

---------------------------------------------
/* Is vector length odd or even ? */
if (length & 0x1)
ans = pCopy[length/2];
else
ans = .5 * (pCopy[length/2 - 1] + pCopy[length/2]);


Well it's the same as (lenght & 1) which will be true when
the least significant bit of the value of length is set. That
means the value in length is odd.

Did you read the comment above the code?
--
Z (Zo**********@daimlerchrysler.com)
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond

Nov 14 '05 #6

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

Similar topics

27
by: ext_u | last post by:
Ok I thought I would try to take the program one thing at a time. (If you remember my last post I am trying to make a histogram with data on the size of each word) Anways first .. I obviously...
27
by: Daniel Vallstrom | last post by:
I'm having problems with inconsistent floating point behavior resulting in e.g. assert( x > 0.0 && putchar('\n') && x == 0.0 ); holding. (Actually, my problem is the dual one where I get...
24
by: Romeo Colacitti | last post by:
Hi, Does anyone here have a strong understanding for the meanings of the terms "lvalue" and "rvalue" as it pertains to C, objects, and different contexts? If so please share. I've been...
44
by: Agoston Bejo | last post by:
What happens exactly when I do the following: struct A { int i; string j; A() {} }; void f(A& a) { cout << a.i << endl;
0
by: Greg | last post by:
In my ASP.NET 1.1 Web application I have an HTTP Module that is a "global exception logger." that logs info about otherwise unhandled exceptions. It has logged the following exception 4 times...
6
by: Geoffrey S. Knauth | last post by:
It's been a while since I programmed in C++, and the language sure has changed. Usually I can figure out why something no longer compiles, but this time I'm stumped. A friend has a problem he...
2
by: Tanari | last post by:
I'm interested in learning about using Context Inversion of Control in c++, but all of the examples I can find use C# or Java. Can someone direct me to an example of Context IoC written in C++? ...
3
by: Michael | last post by:
Hi, I am a little confused about & and int &. Do they both mean reference? int a; int* b= & a ; (& here mean reference of a) int& b = a ; ( How to understand & precisely here?) Thanks...
4
by: David Thielen | last post by:
Hi; We can have an exception thrown in our Application_Start call. And if so, it does then go to Application_Error. However, in this case in Application_Error when we call:...
0
by: Steve | last post by:
Hello- Your assistance with this issue is greatly appreciated. Environment: - Load-balanced IIS 6.0 servers (Win2003) - web servers point (via UNC path) to a Microsoft File Cluster on...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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
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...
0
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,...

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.