'HTML Forms' example usage of logical XOR and bit
I am currently teaching a PHP introductory course to students with some background in HTML, CSS and (very basic/limited) JS. While going over the list of various operators in PHP, I was able to give examples for logical AND and OR in context of processing registration forms:
AND: We want our users to be above a certain age limit AND accept our terms and conditions.
OR: We want new users to pay a registration fee OR refer three other people to register.
While you can frame my OR example as actually being a XOR. I'm looking for a better example, preferably in the context of (registration-like) forms.
Googling it doesn't give much juice. The one example that I somewhat liked is @ http://blogs.msdn.com/b/mvpawardprogram/archive/2013/07/22/excel-2013-real-world-examples-of-new-functions.aspx under "XOR()" (the working half day example).
So, I'm here to ask the community to come up with an example where using XOR in (validating) a registration form would make sense.
And, while we are at it, are there any similar examples for the usage of bitwise operators in general? Where would using bitwise operators make sense, except for compressing many booleans values to save some bytes.
Thanks for any help.
For registration forms, anything that looks like "you must choose something and you can't choose both" is actually a XOR. That still sounds kind of weak to me, but well, registration forms are sort of a limiting context, nothing much happens there.
For bitwise operations in general, the canonical example is checking if a user has all the privileges required to do a certain action, but it's kind of boring and arguably an example of "compressed form of a bunch of booleans" which you specifically did not ask for.
So, here's some more exciting stuff.
Bitboards. Ok, so maybe that's still "a bunch of booleans", but then, any integer is just a bunch of booleans. Bitboards are interesting because there are several techniques with them that exploit the low-level characteristics of addition, subtraction and multiplication. For example, see subtracting a rook from a blocking piece.
SWAR (SIMD within a register, also known by the simpler name "tweaking several bytes at once"), for example this color blending trick, but also adding and subtracting and anything you could build out of combinations of addition and bitwise operations.
Curious algorithms and data structures, such as finding the nearest common ancestor of any two nodes in an oriented forest of trees in O(1) with only O(n) preprocessing, answering range-minimum queries, and Fenwick trees.
Or more common tasks, such as working for bitstreams, which is required for many file formats.
链接地址: http://www.djcxy.com/p/58242.html上一篇: 按位运算符,而不是xor用于分支