-
June 17th, 2004, 03:20 AM
#1
Member
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>>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)
I Speak in frequencies even dogs have trouble hearing
-
June 17th, 2004, 08:58 AM
#2
I'm not incredibly sure how that works, but the >> 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
-
June 17th, 2004, 10:31 AM
#3
Hi
Yes my friend Gothic said it ....They are called Bitwise Operators.....because they operate on individual bits.......
Operator Meaning
~ :::::: One's Compliment
>>:::::: Right Shift
<<:::::: 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>>9) && 0x7f + 1900;
this would be evaluated as ((mdy>>9) && 0x7f) + 1900 so i will maintaiin the sequence
mdy>>9
This would Shift all the bits in mdy nine places to the right
for e.g. if mdy=11010111
then mdy>>1 =01101011
Similarly mdy>>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
|<------- Year-------->|<-----Month----->|<---------day---------->|
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
|<--------Year-------->|
&&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)
{
int i,k,andmask;
for(i=15;i>=0;i--)
{
andmask=1<<i;
k=n&andmask;
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
-
Forum Rules
|
|