scottybwoy Posted February 11, 2008 Share Posted February 11, 2008 Hi I have this function : <?php function isodd($int) { return($int & 1); } ?> It does exactly what it says on the tin. However How does it know that the $int isodd? Can someone explain please, thanks. Link to comment https://forums.phpfreaks.com/topic/90477-function-explanation-quicky/ Share on other sites More sharing options...
scottybwoy Posted February 11, 2008 Author Share Posted February 11, 2008 Basically I don't understand the & bit. Link to comment https://forums.phpfreaks.com/topic/90477-function-explanation-quicky/#findComment-463871 Share on other sites More sharing options...
aschk Posted February 11, 2008 Share Posted February 11, 2008 That is logical AND syntax. e.g. if $int = 4 then in binary it is 00000100 and 1 is 00000001 so doing the logical AND of these gives you: 00000100 00000001 ======= 00000000 Which as you can see is 0, or false... Can you see how it works for an odd number now? Link to comment https://forums.phpfreaks.com/topic/90477-function-explanation-quicky/#findComment-463872 Share on other sites More sharing options...
scottybwoy Posted February 11, 2008 Author Share Posted February 11, 2008 OK, I see. So as it's a logical comparison all binary odd's having the 8th digit as 1 then it returns false whenever there is not a 1 for the 8th bit, correct? Link to comment https://forums.phpfreaks.com/topic/90477-function-explanation-quicky/#findComment-463883 Share on other sites More sharing options...
aschk Posted February 11, 2008 Share Posted February 11, 2008 Yes essentially. So for 87 (01010111) the right most bit is a 1, so doing 1 & 1 (or TRUE AND TRUE) you get TRUE (1) thus working for odd numbers. Link to comment https://forums.phpfreaks.com/topic/90477-function-explanation-quicky/#findComment-463888 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.