473,749 Members | 2,626 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 19836
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 /"\
ericAtiptsoftDc om \ / ASCII Ribbon Campaign
X Against HTML E-Mail
Public Key: 0xBEDF636F / \
Nov 14 '05 #2
agree

"John" <ia******@hotma il.com> дÈëÓʼþ
news:wu******** ************@co mcast.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******@hotma il.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**********@d aimlerchrysler. 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******@hotma il.com> wrote in message news:<wu******* *************@c omcast.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**********@d aimlerchrysler. com> wrote in message
news:ca******** **@news.sns-felb.debis.de.. .
John <ia******@hotma il.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**********@d aimlerchrysler. 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
2622
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 need to determine what a word actually is. I wrote this program on my own without looking at the book or any other resource once. #include <stdio.h> main() {
27
3849
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 failed assertions for assertions that at first thought ought to hold, but that's not important.) At the end is a full program containing the above seemingly
24
2958
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 reading several old posts/threads on the subject, and they never end with a conclusion (people keep correcting each other and disagreeing).
44
2235
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
1073
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 during the past couple of months - and I have no idea what it means. The curious part, to me, is the RAW URL. It is not something that I have set in any links anywhere in the site. I'm wondering what's going on. Here's some of the info logged by...
6
10042
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 hoped I could solve, and I couldn't. Some code he's using, written in 1999, that compiled fine in 1999, no longer does in 2006 with g++ 4. This little bit of code: SimS::SimS (ostream &s) {
2
2626
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++? Thank-you. http://www.gotw.ca/resources/clcm.htm for info about ]
3
2352
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 in advance,
4
3734
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: Server.Transfer("~/error.aspx"); - we get an exception. I assume that since the application is just starting that it is not yet possible to do a transfer. Is there another method in Global.asax that we should do our initialization in? Or another way to send it to...
0
2023
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 different machines. - domain ASPNET account that handles the function/permissions of the (traditionally) local machine ASP.NET account.
0
8997
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, 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...
0
9568
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9256
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6801
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6079
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4709
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...
0
4881
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3320
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
3
2218
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.