GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 05-Aug-2004, 00:03
da_bomb50 da_bomb50 is offline
New Member
 
Join Date: Jul 2004
Posts: 21
da_bomb50 is on a distinguished road

Is there a function to determine whether or not a number is odd or even?


Another question. Is there a function to determine whether or not a number is odd or even?
  #2  
Old 05-Aug-2004, 10:22
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

Testing odd or even with Modulus or Bitwise operators


There is no such function but that isn't really a problem since it's very easy to test this in a single line (with math) or by creating a little custom PHP function that you can use over and over again.

For example:

PHP Code:

<?php

// let's say you have a number
$number = 25;

// to test whether $number is odd you could use the arithmetic
// operator '%' (modulus) like this
if( $odd = $number%2 )
{
    // $odd == 1; the remainder of 25/2
    echo 'ODD Number!';
}
else
{
    // $odd == 0; nothing remains if e.g. $number is 48 instead,
    // as in 48 / 2
    echo 'EVEN Number!';
}
?>


Very often, you will see the comparison (ternary) operator being used to test this ODD / EVEN numbers thingy like this, in one statement:

PHP Code:

<?php
    echo "\$number is $number and it's " . ( ($number % 2) ? 'odd' : 'even' ).'.';
?>


OR, you can even use the bitwise operator '&' to test the number, which is yet another common and popular formula.

PHP Code:

<?php
    echo "\$number is $number and it's " . ( ($number & 1) ? 'odd' : 'even' ).'.';
?>


If you find yourself frequently requiring to test if a number is odd and even, you can write your own custom function to use throughout your scripts, e.g.:

PHP Code:

<?php

if( is_odd($number) )
{
  // do something;
}
else
{
  // do something else
}


// the custom function : is_odd()
// ------------------------------
function is_odd( $int )
{
  return( $int & 1 );
}

?>


Hope that gives you some ideas. If you need to know how the bitwise operator works, please do not hesitate to ask.
  #3  
Old 05-Aug-2004, 18:26
da_bomb50 da_bomb50 is offline
New Member
 
Join Date: Jul 2004
Posts: 21
da_bomb50 is on a distinguished road
Yes, can you please tell me more about the blitwise operator.
  #4  
Old 06-Aug-2004, 07:00
conkermaniac conkermaniac is offline
Member
 
Join Date: Dec 2001
Location: China
Posts: 174
conkermaniac is on a distinguished road
To use an example from the PHP manual:

PHP Code:

echo 12 ^ 9; 


...yields 5. This is how I've always understood this, although I'm sure J can give you the "correct" explanation: 12 in binary form is 1100, while 9 in binary form is 1001. In the eights place, both 12 and 9 are set (equal to 1). In the fours place, only 12 is set. In the twos place, neither are set, and in the ones place, only 9 is set. Since our original expression asks for the expressions that are set in EITHER 12 or 9, but not BOTH, the fours and the ones place is set. This gives us 101, or the number 5.

For J's expression:

PHP Code:

echo $number & 1; 


We are now dealing with a different operator: the "and" operator. Notice how the ones place is ALWAYS set in ODD numbers (7 is 111, 13 is 1101, 21 is 10101, etc.). Also notice how the ones place is NEVER set in EVEN numbers (10 is 1010, 22 is 10110, etc.).

The "and" operator looks for places in which BOTH $number and 1 are set. Since the number "1" is set ONLY in the ones place, the expression will yield either 0 or 1. And in order for it to yield "1", $number must be set in the ones place. And going back to what we discussed earlier, all odd numbers, and only odd numbers, are set in th eones place.

Of course, you know that 1 means true, and 0 means false. If it yields 1, J's script will print "odd", and if it yields 0, it will print "even".

I hope that clarifies things.
__________________
You're not supposed to be looking at this.
  #5  
Old 06-Aug-2004, 21:15
da_bomb50 da_bomb50 is offline
New Member
 
Join Date: Jul 2004
Posts: 21
da_bomb50 is on a distinguished road
I don't really understand what you just said (sorry) so i hope Jds responds.
  #6  
Old 08-Aug-2004, 09:46
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
Conker did explain the bitwise operator well, unfortunately I think you found it confusing simply because you didn't understand how the 'ones' and 'zeroes' in his example actually work. So I'll attempt to explain that before trying to explain how the operator works in this case.

First, about bits.... bits (aka binary digits) are the smallest data item in a computer. A bit can only assume one of 2 values, i.e 0 or 1. Think 'switches' i.e. 0 for OFF and 1 for ON, if you like.

So, the number 47 to you and me looks totally different to a computer; in other words the same number looks like this: 101111, to your computer.

Also, if you don't already know this, a byte is 8 bits, so in my examples below that's what we will be dealing with, a single byte of 8 bits.

Now, to easily understand a byte of 8 bits, we will use a 'table' of 8 columns. Starting from the right and beginning with 1, we will fill each column with the last value x 2. What we're looking for is illustrated below in my super-duper rendition of the table...

Code:
128| 64| 32| 16| 8 | 4 | 2 | 1 ---+---+---+---+---+---+---+--- | | | | | | |

Now if you fill in the bits into these 8 empty columns (from the right), you will see how the number 47 is actually 101111 to our computers. When you fill in each number (1 or 0), into the individual columns, just copy the corresponding heading value off that column only if it's a 1, like this...

Code:
128| 64| 32| 16| 8 | 4 | 2 | 1 ---+---+---+---+---+---+---+--- | | | | | | | 1 = 1 | | | | | | 1 | 1 = 1 + 2 | | | | | 1 | 1 | 1 = 1 + 2 + 4 | | | | 1 | 1 | 1 | 1 = 1 + 2 + 4 + 8 | | | 0 | 1 | 1 | 1 | 1 = 1 + 2 + 4 + 8 (nothing to add here) | | 1 | 0 | 1 | 1 | 1 | 1 = 1 + 2 + 4 + 8 + 32

Guess what "1 + 2 + 4 + 8 + 32" equals?


I'll just list a few binary digits and you can try to figure them out for yourself, before you compare them to the answers below.

a. 10101 = ?
b. 11000000 = ?
c. 111 = ?

The answers:

Code:
128| 64| 32| 16| 8 | 4 | 2 | 1 ---+---+---+---+---+---+---+--- | | | 1 | 0 | 1 | 0 | 1 = 16+4+1 = a. 21 128| 64| 32| 16| 8 | 4 | 2 | 1 ---+---+---+---+---+---+---+--- 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 = 128+64 = b. 192 128| 64| 32| 16| 8 | 4 | 2 | 1 ---+---+---+---+---+---+---+--- | | | | | 1 | 1 | 1 = 4+2+1 = c. 7

Once you understand "bits", we can then try to tackle understanding how the bitwise AND (&) operator works if you still cannot understand Conker's reply above.
  #7  
Old 08-Aug-2004, 21:18
da_bomb50 da_bomb50 is offline
New Member
 
Join Date: Jul 2004
Posts: 21
da_bomb50 is on a distinguished road
Alrighty, i think i understand things a bit better now. Thank you both.
  #8  
Old 09-Aug-2004, 11:31
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
I would like to add a bit of information to JdS's excellent description of bytes and there corresponding bits. What J has described is an unsigined byte. What about the case where you have a signed byte?

Basically, a signed byte can represent numbers from -128 to 127 (for a total of 256). So it can store the same range of numbers as an unsigned byte, but just starts at a different location.

The left most bit is the sign bit in signed storage values (in the case of a byte - the 8th position). As long as J doesn't sue me for copyright infringement , I am going to use his same tables. The positive numbers are easy, just remember that the leftmost bit must be zero.

Code:
128| 64| 32| 16| 8 | 4 | 2 | 1 ---+---+---+---+---+---+---+--- | 1 | | | 1 | | | 1 = 64 + 16 + 1 = 81 | 1 | 1 | 1 | 1 | 1 | 1 | 1 = 1 + 2 + 4 + 8 + 32 + 64 = 127 (limit)

Easy. So let's go to negative numbers. When the sign bit is set we have a negative number, so it should be this easy, right?

Code:
128| 64| 32| 16| 8 | 4 | 2 | 1 ---+---+---+---+---+---+---+--- 1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 = -1 ???

Wrong! Negative numbers are stored in what is refered to as 2's complement values. In order to interpret a signed byte, you must apply a 2's complement calculation to it.

So what is 2's complement? Easy. It is the 1's complement with 1 added to it! Okay, so what is the 1's compliment you ask? It is the exact inverse of the bits. So where a bit is switched on, it needs to be switched off. So take the byte 1000001 and do the 1's complement:

Code:
10000001 --------- 01111110

Now add 1 to get the 2's complement;

Code:
01111110 + 00000001 ----------- = 01111111 = 127

So the result is -127.

So what is decimal -1 in a signed byte? 11111111.... (seems wrong doesn't it.)

Code:
11111111 - original signed byte 00000000 - 1's complement 00000001 - 2's complement.

There you have it. Hopefully this doesn't cloud the issue too much, but it is important to know wether you are working with a signed or unsigned value....

One last thing - where does -128 come from? In 8-bit binary it is 10000000. So how does that work?

Code:
10000000 - original signed byte 01111111 - 1's compliment ... 01111111 + 00000001 ---------- = 10000000 - Remember that bits carry on arithmetic...

The unsigned value turns out to be 128, so the result is -128.
  #9  
Old 10-Aug-2004, 00:35
da_bomb50 da_bomb50 is offline
New Member
 
Join Date: Jul 2004
Posts: 21
da_bomb50 is on a distinguished road
Ok, that makes sense. Thank you.
  #10  
Old 10-Aug-2004, 09:53
conkermaniac conkermaniac is offline
Member
 
Join Date: Dec 2001
Location: China
Posts: 174
conkermaniac is on a distinguished road
Oh, I'm sorry. I didn't realize that you didn't understand the 1's and 0's part.

...come to think of it, my explanation is pretty darn hard to understand, a few days later.

Conker
__________________
You're not supposed to be looking at this.
 
 

Recent GIDBlogNARMY by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 23:51.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.