0

I am new to C++ and have programming knowledge only in Java. Can anyone please explain the meaning of the code below:

#define DEF  134 ;
int k;
char msk;
PMSK *pm;  //At the begining of class some declaration

// i is some counter(int)
if ( (nVersion >= 2004) && (nVersion < 2008)) 
{              
    k = to_bits(DEF, &msk);  
    if ( pm->orbits[k] & msk )              // for version >= 2004
    {
        x = atoi( grprs[i] );
        if ( !defpair( i, x ) )
           pm->orbits[k] &= 0xFF ^ msk;     // turn off bit 
    }
}

to_bits() is method which will return an integer value and a (char) msk value (example 1000). It has bit operations involved in it.

What is pm->orbits[k]? Can we replace it in Java like pm.orbits[k]? Also, what exactly the last line of code doing?

Ryan Wersal
  • 3,210
  • 1
  • 20
  • 29
JavaBits
  • 2,005
  • 11
  • 36
  • 40
  • 1
    `#define DEF 134 ;` ...Remove the semicolon, or else your code wouldn't compile. – Nawaz Apr 26 '11 at 06:47
  • http://stackoverflow.com/questions/5577225/explain-the-following-c-code-part – atzz Apr 26 '11 at 06:59
  • Thanks for your answers. Im not getting the meaning of the below line- if ( pm->orbits[k] & msk ) - when this condition can be true?This is a AND operation so if 100001 & 100100 - are the 2 values then wat will happen? Please explain. – JavaBits Apr 26 '11 at 09:18

4 Answers4

2

What is pm->orbits[k]? can we replace it in java like pm.orbits[k]?

Basically, yes. The -> operator de-references and then access a field (also known as access the field/function of the object pointed to by the pointer). However, if you had a reference type to begin with, you get the de-referencing "for free".

PMSK *pm1; // assume this has been initialized to point to something valid
PMSK &pm2; // assume this is a valid reference
PMSK pm3; // assume this is a valid declaration

pm1->orbits[0]; // accesses field orbits[0] of object pointed to by pm1
(*pm1).orbits[0]; // equivalent to above statement

pm2.orbits[0]; // it's implicitly understood that de-referencing should take place
pm3.orbits[0]; // no need to dereference

Dissecting the last line of code:

pm->orbits[k] &= 0xFF ^ msk;     // turn off bit 
  1. ^ is the bitwise exclusive or operator (a.k.a. xor). Basically it returns a bit value of 1 if both bits are not equal and 0 otherwise.

  2. &= is the bitwise-and assigment operator. Equivalent to the following:

    pm->orbits[k] = pm->orbits[k] & (0xFF^msk);
    
    The bitwise and operator matches up equivalent bits and determines if both are 1. If they are, the result is 1. Otherwise, it's 0. So 100001 & 100100 = 100000 (binary numbers). So it takes whatever's in msk, toggles the lowest 8 bits (1 -> 0 and 0 -> 1), then bitwise-ands that with the current pm->orbits[k] field. Finally, it assigns the result back to pm->orbits[k]

In Java, it's required to have an explicit check to somehow convert the results from a number to a boolean. However, in C++ it's implicitly understood that anything which isn't 0 is true.

if(1) // same as if(1!=0)
if(2) // same as if(2!=0)
if(0) // same as if(0!=0)
if(-1) // same as if(-1!=0)
helloworld922
  • 10,801
  • 5
  • 48
  • 85
  • Thanks for your answer. Im not getting the meaning of the below line- if ( pm->orbits[k] & msk ) - when this condition can be true?This is a AND operation so if 100001 & 100100 - are the 2 values then wat will happen? Please explain. – JavaBits Apr 26 '11 at 09:16
  • I clarified what bitwise and does and why you can put numbers into an if condition and still get working code. – helloworld922 Apr 26 '11 at 15:13
1

Yes you can replace the code you mention. The '->' operator dereferences a pointer to a PMSK object. Java doesn't have pointers, only references, but they are used in the same way.

The last line is clearing the bits set in msk, however it's normally done like this:

pm->orbits[k] &= ~msk;

If you want to set the bits in msk, you'd use:

pm->orbits[k] |= msk;

Also, the following line will need to evaluate to a boolean expression, where as in C++ it just needs to be non-zero:

if ((pm->orbits[k] & msk) != 0)
{
    ...
} 
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • can you elaborate a little more . What do you mean by clearing the bits set. if msk has 1000 value. Then what will happen? can you give small example just to explain it may not be accurate. Thanks for your help. – JavaBits Apr 26 '11 at 06:53
  • The same operations exist in Java. If you look at the bits within an integer, `&= ~msk` will clear whatever bits are currently set in `msk`. The result is stored in `pm->orbits[k]`. So if `0x1000` is currently set then it will contain `0x0000`. If it's currently set to `0x1001` then it will contain `0x0001` etc. – trojanfoe Apr 26 '11 at 06:56
  • Thanks for your answer. Im not getting the meaning of the below line- if ( pm->orbits[k] & msk ) - when this condition can be true?This is a AND operation so if 100001 & 100100 - are the 2 values then wat will happen? Please explain. – JavaBits Apr 26 '11 at 09:18
  • The result will be true if any bits on either side of the operation are BOTH set. With the two values you give, bit-6 (0x100000) is set in both, so the if statement will be entered. – trojanfoe Apr 26 '11 at 09:23
  • So when any of the common bit is set the if condition will be entered . Here it is bit 6 .right? Thanks a lot. – JavaBits Apr 27 '11 at 09:19
  • Yes, that's right. bit-1 is the least significant bit (LSB), on the far right. – trojanfoe Apr 27 '11 at 09:27
0

What is pm->orbits[k]? can we replace it in java like pm.orbits[k]?

Yes, pm is a pointer, and the -> notation is how to access members on a pointer.

The last line is a bitwise operation, called an exclusive or, the ^ operator is the same and does the same thing in Java.

Binary Worrier
  • 50,774
  • 20
  • 136
  • 184
0

The -> operator dereferences a pointer to select a field.

The last line of code does a bitwise exclusive or (XOR) of the msk value with 0xFF. This value is then bitwise ANDed with the pm->orbits array value at index k.

A few subjects that are going to help as you learn C++ and work with this code

Just about any introductory C++ text will have this info.

ccozad
  • 1,119
  • 8
  • 13