not understandable codesnippet

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Bomas
Forum Newbie
Posts: 24
Joined: Sun Oct 17, 2004 2:41 am
Location: Heverlee, Belgium

not understandable codesnippet

Post by Bomas »

hi all,
i need a little help with this code, i can't understand what it says:

Code: Select all

$second=251; 
$tmp = ($second & 16) > 0 ? ( ($second &  > 0 ? 1 : 2 ) : ( ($second &  > 0 ? 0 : 2.5 ) 
// $tmp returns: 1
i've seen these kinds of code before, but i've never had to work with them. Can somebody help me plz, i need this badly

Greetz,
Bomas
Last edited by Bomas on Sun Jan 01, 2006 9:00 am, edited 1 time in total.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

The only two things you need to read to understand this line of code:

http://be.php.net/manual/en/language.op ... itwise.php
http://be2.php.net/ternary
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Code: Select all

if (($second & 16) > 0) {
  if (($second &  > 0) {
    $tmp = 1;
  } else {
    $tmp = 2;
  }
} else {
  if (($second &  > 0) {
     $tmp = 0;
  } else {
     $tmp = 2.5;
  }
}
Think that's right .. had a bit to drink with lunch..
User avatar
Bomas
Forum Newbie
Posts: 24
Joined: Sun Oct 17, 2004 2:41 am
Location: Heverlee, Belgium

Post by Bomas »

i'm sorry, but i don't get the idea.
what does it do exactly, can you explain it for the first part of my codesnippet:

Code: Select all

$second=251;
$tmp = ($second & 16) > 0? "a": "b");
//$tmp returns "a"
what does the operator do? php.net says: "Bits that are set in both $a and $b are set."
i don't get it.

and what does this line means: "Bitwise operators allow you to turn specific bits within an integer on or off".

thanx allready fr helping me out

Greetz,
Bomas
User avatar
trukfixer
Forum Contributor
Posts: 174
Joined: Fri May 21, 2004 3:14 pm
Location: Miami, Florida, USA

Post by trukfixer »

Bomas wrote:i'm sorry, but i don't get the idea.
what does it do exactly, can you explain it for the first part of my codesnippet:

Code: Select all

$second=251;
$tmp = ($second & 16) > 0? "a": "b");
//$tmp returns "a"
what does the operator do? php.net says: "Bits that are set in both $a and $b are set."
i don't get it.

and what does this line means: "Bitwise operators allow you to turn specific bits within an integer on or off".

thanx allready fr helping me out

Greetz,
Bomas
ternaries are just a simple shorthand way of saying "if true, do this, else do this" all on one line

So - assuming $second is 251, look at it this way

$tmp is the returned value
$second is 251, and therefore it is > (greater than) 0 so that part of the bitwise is true , and since the second part is 16 , that's also true, so teh entire equation is true, and teh first conditional applies (if a and b are both > 16 return a , else return b)

try setting $second to 3 and see what happens ;)

Code: Select all

$two = 251;
$three = 3;
$tmp = (($two & 16) > ?'a':'b';
$tmp2 = (($three & 16)> ?'a':'b';
echo "<br>two = $tmp<br>";
echo "three = $tmp2";
//outputs 
//two = a
//three = b
In ither words read a ternary like this:

if condition (condition within brackets, *implies* "if") then ( ? ) return this else ( : ) return this

the ? is like saying "if the condition is *true* , return this value, otherwise go to the : and return THAT value "
sheila
Forum Commoner
Posts: 98
Joined: Mon Sep 05, 2005 9:52 pm
Location: Texas

Post by sheila »

what does the operator do? php.net says: "Bits that are set in both $a and $b are set."
i don't get it.


and what does this line means: "Bitwise operators allow you to turn specific bits within an integer on or off".
Maybe it'll help to see it written out this way. Compare the individual bits of both numbers to get the result. When the bits are different the result is a 0, when they are both 1 the result is a 1.

Code: Select all

binary         decimal

   1111 1011        251
&  0001 0000      &  16
   ------------    ----
   0001 0000         16

   1111 1011        251
&  0000 1000      &   8
   ------------    ----
   0000 1000          8

   1111 1011        251
&  0000 0100      &   4
   ------------    ----
   0000 0000          0
User avatar
Bomas
Forum Newbie
Posts: 24
Joined: Sun Oct 17, 2004 2:41 am
Location: Heverlee, Belgium

Post by Bomas »

thx sheila, that's what i didn't understand.
Srry for not explaining my problem a little more, the short version of if-structures i understand perfectly well, i use them all the time, but i did'nt understand the binary comparison

Thx a lot

Greetz,
Bomas
Post Reply