Hi Folk
I came accross this very simple function, but I have no idea how it works
(the ampersands). Can you explain it.
function is_even($num){
return (is_numeric($num)&(!($num&1)));
}
TIA
Nicolaas 7 1181
Heh, nice solution! :)
The 1st statement checks, if $num is a number.
The 2nd one checks, if the the binary "one" is set, as it is with every
odd number.
Alternatively, you could use modulo:
!($num % 2)
IMO, the most odd thing about this is the single "&" between the two
statements; I would use "&&" or "and" here. ;)
Cheers..
milahu wrote: Heh, nice solution! :)
The 1st statement checks, if $num is a number.
The 2nd one checks, if the the binary "one" is set, as it is with every odd number. Alternatively, you could use modulo: !($num % 2)
IMO, the most odd thing about this is the single "&" between the two statements; I would use "&&" or "and" here. ;)
Why would you do that? '&&' and '&' are completely different operators,
and give different results when evaluated in boolean context on two numbers.
Tim
Tim Martin wrote: milahu wrote: IMO, the most odd thing about this is the single "&" between the two statements; I would use "&&" or "and" here. ;)
Why would you do that? '&&' and '&' are completely different operators, and give different results when evaluated in boolean context on two numbers.
Because I prefer comparing boolean values with logical instead of byte
operators.
milahu wrote: Tim Martin wrote: milahu wrote: IMO, the most odd thing about this is the single "&" between the two statements; I would use "&&" or "and" here. ;) Why would you do that? '&&' and '&' are completely different operators, and give different results when evaluated in boolean context on two numbers.
Because I prefer comparing boolean values with logical instead of byte operators.
Sorry, I misread you. I thought you were referring to replacing the
second '&', not the first one. I didn't even spot that the two boolean
expressions were being combined with a bitwise operator (which I agree
is wrong).
For reference, the function in question was:
function is_even($num){
return (is_numeric($num)&(!($num&1)));
}
Tim
windandwaves wrote:
Thanks folk
It makes more sense now. I gather that the single ampersand test for a
particular bit in a byte, while the double ampersand is the AND operator.
Thanks a million.
Nicolaas
On Fri, 21 Apr 2006 11:20:27 +0100, Tim Martin wrote: milahu wrote: Tim Martin wrote: milahu wrote: IMO, the most odd thing about this is the single "&" between the two statements; I would use "&&" or "and" here. ;) Why would you do that? '&&' and '&' are completely different operators, and give different results when evaluated in boolean context on two numbers.
Because I prefer comparing boolean values with logical instead of byte operators.
Sorry, I misread you. I thought you were referring to replacing the second '&', not the first one. I didn't even spot that the two boolean expressions were being combined with a bitwise operator (which I agree is wrong).
For reference, the function in question was:
function is_even($num){ return (is_numeric($num)&(!($num&1))); }
Tim
Why is it wrong? you're anding 0 or 1 with 0 or 1, and that'll return 1
only if both are 1. It might even be faster - probably not though.
Steve wrote: On Fri, 21 Apr 2006 11:20:27 +0100, Tim Martin wrote: Sorry, I misread you. I thought you were referring to replacing the second '&', not the first one. I didn't even spot that the two boolean expressions were being combined with a bitwise operator (which I agree is wrong).
For reference, the function in question was:
function is_even($num){ return (is_numeric($num)&(!($num&1))); }
Why is it wrong? you're anding 0 or 1 with 0 or 1, and that'll return 1 only if both are 1. It might even be faster - probably not though.
It's wrong in the sense of being a bad habit, not in the sense of
returning the wrong result. Doing this sort of thing regularly can lead
to brittle code that breaks due to seemingly innocuous changes.
Tim This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jonas |
last post by:
This works fine in Win XP but does not work at all in Win 98.
Private WithEvents objIExplorer As InternetExplorer
I have to do it like this to get it to work in Win 98
Dim objIExplorer As...
|
by: me |
last post by:
I have a Class Library that contains a Form and several helper classes. A
thread gets created that performs processing of data behind the scenes and
the Form never gets displayed (it is for debug...
|
by: Anoop |
last post by:
Hi,
I am new to this newsgroup and need help in the following questions.
1. I am workin' on a GUI application. Does C# provides Layout Managers the
way Java does to design GUI? I know that it...
|
by: Cuthbert |
last post by:
After compiling the source code with gcc v.4.1.1, I got a warning
message:
"/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the
'gets' function is dangerous and should not be...
|
by: webEater |
last post by:
I have a problem, it's not browser specific, and I don't get a
solution. I have an (X)HTML document, I show you a part of it:
....
<!--<div class="pad">-->
<div id="eventImages"><img src=""...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |