TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 09-10-2007, 10:31 AM   #1 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default Challenge Yourself!

Just thought I'd give people a little challenge, have a try at this Zend certification practice question. It's a tricky one if you don't know how to use the mathematical and bitshift operators properly.

PHP Code:
$a    10;
$b    20;
$c     4;
$d    8;
$e    1.0;

$f $c $d 2;
$g $f 20;
$h $b $a $c 2;
$i $h << $c;
$j $i $e;

echo 
$j
What does $j output?


Do not scroll down if you don't want to see peoples' answers


ok, now I've put that warning there, feel free to post your answers :)

Last edited by Wildhoney : 09-11-2007 at 11:55 AM. Reason: added warning
Karl is offline  
Reply With Quote
Old 09-10-2007, 10:48 AM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,381
Thanks: 5
Salathe is on a distinguished road
Default

* Salathe raises hand, "I know, I know!"
Salathe is offline  
Reply With Quote
Old 09-10-2007, 11:31 AM   #3 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

lol, I knew you'd have it figured out in no time :)
Karl is offline  
Reply With Quote
Old 09-11-2007, 03:52 AM   #4 (permalink)
The Wanderer
 
aSAP's Avatar
 
Join Date: Sep 2007
Posts: 7
Thanks: 0
aSAP is on a distinguished road
Default

Not sure but is it 128?
aSAP is offline  
Reply With Quote
Old 09-11-2007, 10:53 AM   #5 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

Here's the solution

PHP Code:

$a    
10;
$b    20;
$c     4;
$d    8;
$e    1.0;

$f $c $d 2;
$g $f 20;
$h $b $a $c 2;
$i $h << $c;
$j $i $e;

echo 
$j
Ok, so the best way to work this out is to work from the end back to the beginning. So first we look at out outcome variable $j and work back.

So, $j is equal to $i * $e, we know $e is 1.0, so we only need to worry about $i.

We can see that $i is equal to:

PHP Code:
$i $h << $c 
Since $h holds the result of a previous calculation, we must first discover its value before we can find the value of $i.

PHP Code:
$h $b $a $c 
Now, we know $b is 20, $a is 10 and $c is 4 (as these variables were initialised with these values) so the previous statement becomes:

PHP Code:
$h 20 10 
which equals 16, so we now know that

PHP Code:
$h 16 
Ok so we have our $h, we can now calculate $i:

PHP Code:
$i $h << $c 
or

PHP Code:
$i 16 << 
Which means shift 16 4 bits to the left (Im not gunna bog you down with bit manipulation here, I'll release an article on this soon), so we can now determine that:

PHP Code:
16  << 32
32  
<< 64
64  
<< 128
128 
<< 256 
so therefor:

PHP Code:
$i 256 
Finally, now we know the value of $i, we can now determine the value of $j:

PHP Code:
$j $i $e 
or

PHP Code:
$i 256 
so, the answer to this little mystery is 256!
Karl is offline  
Reply With Quote
Old 09-11-2007, 11:37 AM   #6 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,381
Thanks: 5
Salathe is on a distinguished road
Default

PHP Code:
$a    10;
$b    20;
$c     4;
$d    8;
$e    1.0;

$f $c $d 2;      // 4 + (8 * 2) = 20
$g $f 20;          // 20 % 20 = 0 [modulus, a%b = remainder of a/b]
$h $b $a $c 2// 20 - 10 + 4 + 2 = 16
$i $h << $c;         // 16 << 4 = 16 * 2^4 = 16 * (2*2*2*2) = 256
$j $i $e;          // 256 * 1.0 = 256.0 [int * float = float]

echo $j;               // 256  [echoing float with no decimals] 
Edit: oops didn't see that Karl put up the answer. Oh and remember that int * float = float, Karl's post doesn't make that clear. :)
Salathe is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 06:45 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design