Skip to content

Instantly share code, notes, and snippets.

@amcgowanca
Created March 6, 2013 00:54
Show Gist options
  • Save amcgowanca/5095828 to your computer and use it in GitHub Desktop.
Save amcgowanca/5095828 to your computer and use it in GitHub Desktop.
Fanshawe College, INFO-5094: LAMP 2, Project 1 - IteratorMode examples of using Bitwise operators
<?php
class IteratorMode
{
const KEEP = 1;
const DELETE = 2;
const FIFO = 4;
const LIFO = 8;
public static function isKeep($mode)
{
return 1 == ($mode & self::KEEP);
}
public static function isFifo($mode)
{
return 4 == ($mode & self::FIFO);
}
}
// Example usage ...
$keep_and_fifo = IteratorMode::KEEP | IteratorMode::FIFO;
if (IteratorMode::isKeep($keep_and_fifo)) {
// Yay! Our iterator will not delete nodes as we traverse.
}
$xored_keep_and_fifo = IteratorMode::DELETE ^ IteratorMode::FIFO;
if (IteratorMode::isFifo($xored_keep_and_fifo)) {
// Awesome! We are moving forward, first-in, first-out
}
@amcgowanca
Copy link
Author

Note: The IteratorMode::DELETE ^ IteratorMode::FIFO could also be an OR instead of XOR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment