11150

meaning of & in C++

Question:

I am bit confused about a expression I found in some C++ code:

if (m & 1) pmm=-pmm;

I am not a C/C++ coder, so Google gives me two things:

<ol><li>& is bitwise AND</li> <li>if syntax is if (condition) statement</li> </ol>

So, how does the above statement work? Should I not require a if ((m & 1)==0)?

Answer1:

Just to add to the more technical explanations, a simpler way of viewing if (m & 1) is that is tests whether m is <em>odd</em> (i.e. not an exact multiple of 2):

if (m & 1) // m is odd (1, 3, 5, 7, 9, ...) - do something else // m is even (0, 2, 4, 6, 8, ...) - do something else

Answer2:

<strong>m</strong> will be represented in memory as a binary number, in other words: a series of bits. The value <strong>1</strong> will also be represented as a series of bits.

For example (I have chosen 8 bit length for simplicity):

m = 00101101 1 = 00000001

Performing a bitwise operation on <strong>m</strong> will perform the operation you specify, in this case <strong>&</strong> (AND) on each bit at the same position on each binary number on either side of the <strong>&</strong> symbol.

<em>i.e. Bit 1 of result = Bit 1 of m & Bit 1 of the value 1. Bit 2 of result = Bit 2 of m & Bit 2 of the value 1 etc...</em>

So for our example:

00101101 & 00000001 --------- 00000001

Assuming <strong>m</strong> is an integer, the bitwise operation will return an integer. The <strong>if</strong> statement will check if the result is true, and since it is an integer it will be interpreted as true if non-zero.

The result is not zero hence it will return true in our example.

So: By <strong>AND</strong>-ing an integer with 1 you will end up returning true in your code for odd numbers only, since bit 1 is always 1 for odd numbers.

Answer3:

This is bitwise AND operator. m & 1 evaluates to 1 if bit 0 is set and every expression that is not 0 is <strong>true</strong> implicitly.

Following are equivalent expressions:

if ((m & 1) == 1) // bitwise AND is 1 if ((m & 1) != 0) // bitwise AND is NOT 0 if (m & 1)

However caution is required if testing more bits. For example m = 1:

if (m & 3) is true also, but the result is 1. Better is if ((m & 3) == 3) in this case.

Recommend

  • Shake iPhone to open my app. Possible?
  • float:left paragraph followed by float:none paragraph
  • Is there a filesystem plugin available for django?
  • Set up the Android-Facebook sdk for Netbeans
  • How to setCenter mapview with location in google maps sdk for iOS
  • How to fix this floating point square root algorithm
  • C++ exception safety paranoia: how much is too much?
  • Which table should be Parent table and which should be child table?
  • Best Datagrid for Bootstrap/jQuery?
  • Enable Bootstrap Intellisense using Angular 4 in WebStorm
  • WebApp in AppServices vs CloudService
  • Android application not restoring state when installed from .apk, works fine from eclipse
  • How to remove single character words from string with preg_replace
  • css background images not always displayed
  • Backward compatibility of Python 3.5 for external modules
  • Request response issues in biztalk
  • Python cosine function precision [duplicate]
  • HttpClient: disabling chunked encoding
  • Sensibility of combined Maven/Ant+Ivy build management for dual platform Desktop/Android deployment?
  • Hibernate to update table schema
  • How to create CGPath from a SKSpriteNode in SWIFT
  • Git describe fails to return most recent annotated tag
  • Trying to get the char code of ENTER key
  • Position: fixed nav does not stay fixed
  • How do I exclude a dependency in provided scope when running in Maven test scope?
  • Abort upload large uploads after reading headers
  • D3 nodes and links from JSON with nested arrays of children
  • Asynchronous UI Testing in Xcode With Swift
  • Is there a javascript serializer for JSON.Net?
  • angularjs unit test when to use $rootScope.$new()
  • Where to put my custom functions in Wordpress?
  • what is the difference between the asp.net mvc application and asp.net web application
  • How do you troubleshoot character encoding problems?
  • Buffer size for converting unsigned long to string
  • Angular 2 constructor injection vs direct access
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • MATLAB: Piecewise function in curve fitting toolbox using fittype
  • Binding checkboxes to object values in AngularJs
  • UserPrincipal.Current returns apppool on IIS