![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
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
|
||||
|
||||
Testing odd or even with Modulus or Bitwise operatorsThere 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:
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:
OR, you can even use the bitwise operator '&' to test the number, which is yet another common and popular formula. PHP Code:
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:
Hope that gives you some ideas. If you need to know how the bitwise operator works, please do not hesitate to ask. __________________
J de Silva Learning Journal | GIDForums™ | GIDNetwork™ | GIDWebhosts™ | GIDSearch™ |
|
#3
|
|||
|
|||
|
Yes, can you please tell me more about the blitwise operator.
|
|
#4
|
|||
|
|||
|
To use an example from the PHP manual:
PHP Code:
For J's expression: PHP Code:
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
|
|||
|
|||
|
I don't really understand what you just said (sorry) so i hope Jds responds.
|
|
#6
|
||||
|
||||
|
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:
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:
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:
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. __________________
J de Silva Learning Journal | GIDForums™ | GIDNetwork™ | GIDWebhosts™ | GIDSearch™ |
|
#7
|
|||
|
|||
|
Alrighty, i think i understand things a bit better now. Thank you both.
|
|
#8
|
||||
|
||||
|
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:
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:
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:
Now add 1 to get the 2's complement; Code:
So the result is -127. So what is decimal -1 in a signed byte? 11111111.... (seems wrong doesn't it.) Code:
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:
The unsigned value turns out to be 128, so the result is -128. __________________
The best damn Sports Blog period. |
|
#9
|
|||
|
|||
|
Ok, that makes sense. Thank you.
|
|
#10
|
|||
|
|||
|
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 GIDBlog
Toyota - 2009 May Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The