awebster Posted August 14, 2012 Share Posted August 14, 2012 What does the &1 mean after the variable $num? What is the specific purpose of it? Quote Link to comment https://forums.phpfreaks.com/topic/267084-php-num1/ Share on other sites More sharing options...
requinix Posted August 14, 2012 Share Posted August 14, 2012 Where? Is it like $num & 1? That's a bitwise AND (which is sometimes misused in PHP to be a boolean AND, but probably not in this case). Quote Link to comment https://forums.phpfreaks.com/topic/267084-php-num1/#findComment-1369431 Share on other sites More sharing options...
awebster Posted August 14, 2012 Author Share Posted August 14, 2012 I had it in my subject of the post $num&1. I am using it to find if a number is odd or even and I am wondering if it is just like what a modulus does. Quote Link to comment https://forums.phpfreaks.com/topic/267084-php-num1/#findComment-1369451 Share on other sites More sharing options...
requinix Posted August 14, 2012 Share Posted August 14, 2012 I had it in my subject of the post $num&1. I am using it to find if a number is odd or even and I am wondering if it is just like what a modulus does. Eh, so maybe I don't pay much attention to thread titles... It is kinda like modulus but only for powers of 2 (which includes 2^0=1). For any other number it works differently. Check the link I gave. Quote Link to comment https://forums.phpfreaks.com/topic/267084-php-num1/#findComment-1369457 Share on other sites More sharing options...
awebster Posted August 15, 2012 Author Share Posted August 15, 2012 Ok thanks. I have read the info on the wikipedia page and still don't understand it that well. Quote Link to comment https://forums.phpfreaks.com/topic/267084-php-num1/#findComment-1369479 Share on other sites More sharing options...
Pikachu2000 Posted August 15, 2012 Share Posted August 15, 2012 In what context is code that being used? Quote Link to comment https://forums.phpfreaks.com/topic/267084-php-num1/#findComment-1369483 Share on other sites More sharing options...
awebster Posted August 15, 2012 Author Share Posted August 15, 2012 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>is_numeric()</title> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> </head> <body> <?php $num = rand(); echo('<p>'. round($num) . "</p>"); if (is_numeric ($num)) { echo "<p>Yes it is a number.</p>"; } else { echo "<p>No it is not a number.</p>"; } if(($num&1)) { echo '<p>The number is odd.</p>'; } else { echo '<p>The number is even</p>'; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/267084-php-num1/#findComment-1369488 Share on other sites More sharing options...
kicken Posted August 15, 2012 Share Posted August 15, 2012 Ok thanks. I have read the info on the wikipedia page and still don't understand it that well. I'm not really sure how one would explain it any better than the wiki page, they do a pretty good job of explaining how it works, with plenty of examples. Basically, as you (should) know, all data at the core is just a string of 1 and 0 bits. So a number like 16 is represented as 00010000. The number 1 is represented as 00000001. What & does is examine each of those bits and generates a third bit pattern (new data) based on the other two. For each bit position, it examines the value at that point in the two input numbers. If they are both 1, the output pattern has a 1, otherwise it has a 0. So given 16 & 1: 00010000 & 00000001 ---------------- 00000000 The reason this works for identifying if a number is even or odd is because in binary form, all even numbers have a 0 in the right-most bit, and all odd numbers have a 1 there. By applying &1 to a number you are able to test if that bit is set to 1 or 0, thus identifying if it is odd or even. Quote Link to comment https://forums.phpfreaks.com/topic/267084-php-num1/#findComment-1369492 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.