Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/267084-php-num1/#findComment-1369457
Share on other sites

<!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>

Link to comment
https://forums.phpfreaks.com/topic/267084-php-num1/#findComment-1369488
Share on other sites

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.

 

Link to comment
https://forums.phpfreaks.com/topic/267084-php-num1/#findComment-1369492
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.