weird code line

1. ## weird code line

Was looking at some C++ code and saw a line of code that appeared a little bit weird. Wonder if someone around could have an idea, mdy is a variable:

y = (mdy&gt;&gt;9) && 0x7f + 1900;

this is part of a simple code that calculates julian dates out of our calendar and the date is introduced as a string variable, ie: 02121987 (february 12 1987)

2. I'm not incredibly sure how that works, but the &gt;&gt; is a binary shift right AFAIK and && is the and operator. I believe that when you use && like that it compares each bit and ands it, but I could be wrong.

Doubt that really helps you understand what it's doing though. Worth a try

ac

3. Hi

Yes my friend Gothic said it ....They are called Bitwise Operators.....because they operate on individual bits.......

Operator Meaning

~ :::::: One's Compliment

&gt;&gt;:::::: Right Shift

&lt;&lt;:::::: Left Shift

& :::::: Bitwise "AND"

| :::::: Bitwise OR

^ :::::: Bitwise XOR

Can we have the whole code please.......

until then Lest see what this code here does..it's performing some operation on the year part of the date(my interpretation of the code i could be wrong) ..........

y = (mdy&gt;&gt;9) && 0x7f + 1900;

this would be evaluated as ((mdy&gt;&gt;9) && 0x7f) + 1900 so i will maintaiin the sequence

mdy&gt;&gt;9

This would Shift all the bits in mdy nine places to the right

for e.g. if mdy=11010111
then mdy&gt;&gt;1 =01101011
Similarly mdy&gt;&gt;2=00110101

I belive by right shift what ists trying to do is get the Year out of the variable mdy(month day Year)

it should be something like this

Code:
```
0  0   0   1   0  1   0    0    0    1   1   0    1     0    0   1

|&lt;------- Year--------&gt;|&lt;-----Month-----&gt;|&lt;---------day----------&gt;|

If we right Shift By 9 we Get the Yesr

0  0   0   0   0  0   0    0    0    0   0   0    1     0    1   0

|&lt;--------Year--------&gt;|

```

&&0x7f

"I believe that when you use && like that it compares each bit and ands it, but I could be wrong."

"&&" is a Logical "AND" operator Not Bitwise.......i am not sure if it would be evaluated as the bitwise operator "&"..i will check it as soon as get my hands on a C++ compiler........

ok made a small program to test that and Gothic sorry but you seem to be wrong there from what i am getting......works in TurboC++ can't take responcibility or any other C++ compilers.......

Code:
```#include<iostream.h>
#include<conio.h>

showbits(int);

void main()
{
int i,j,k;

k=9;
i=40;

clrscr();

showbits(i);
cout<<endl;
showbits(k);
cout<<endl;

j=i&k;
//j=i&&k;
showbits(j);
cout<<endl;

getch();
}

//This Function Will Print the Binary of any Integer
or Character that is Passed to it as the parameter.....
showbits(int n)
{

for(i=15;i>=0;i--)
{

k==0?cout<<"0":cout<<"1";
}
return;
}

Output

case 1 ::   i & j

The Result :: i=0000000000101000
j=0000000000001001
-------------------
0000000000001000

Case 2 :: i && j

The Result :: i=0000000000101000
j=0000000000001001
--------------------
0000000000000001

```
+ 1900

I don't think it needs any explaination..............

-Good Luck--

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•