Rotating bits, one shift at a time.
Posts tagged Code
ore-may ig-pay atenizing-lay
Dec 7th
I got bored. So sue me. Redone in C++ (correctly this time, and probably more efficient than this or this.)
#include <iostream> #include <string> #include <vector> #include <cctype> using namespace std; bool isPunctuation( char c ); string latinize( string s ); bool isVowel( char c ); int main( ) { string input, output, temp; vector<string> words; vector<char> punctuation; cout< < "Enter a phrase you would like to latenize" << endl << "-> "; getline( cin, input ); for( int i = 0; i < input.length(); i++ ) { char c = tolower( input.at( i ) ); if( isPunctuation( c ) ) { words.push_back( temp ); punctuation.push_back( c ); temp.clear(); } else temp.append( 1, c ); } if( !temp.empty() ) { words.push_back( temp ); punctuation.push_back( ' ' ); } for( int i = 0; i < words.size(); i++ ) { string l = latinize( words.at( i ) ); output.append( l ); output.append( 1, punctuation.at( i ) ); } cout<< output << endl; return 0; } bool isPunctuation( char c ) { char punctuation[33] = { ',', '.', '/', '<', '>', '?', ';', '\'', ':', '"', '[', ']', '\\', '{', '}', '|', '`', '-', '=', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', ' ' }; for( int i = 0; i < 33; i++ ) if( c == punctuation[i] ) return true; return false; } string latinize( string s ) { string postfix, temp; for( int i = 0; i < s.length(); i++ ) { char c = s.at( i ); if( isVowel( c ) ) { postfix.append( "ay" ); temp = s.substr( i ); temp.append( "-" ); temp.append( postfix ); return temp; } else postfix.append( 1, c ); } } bool isVowel( char c ) { char vowels[5] = { 'a', 'e', 'i', 'o', 'u' }; for( int i = 0; i < 5; i++ ) if( c == vowels[i] ) return true; return false; }
Cryptologically Speaking
Dec 3rd
I’ve given myself a quest, a quest to find the most secure method to get a user to register, and login to a site, and make sure it’s still them as they explore. This is both surprisingly simple, just complicated because of the amount of steps involved, but depending on who you host with, could also get quite expensive. More on that later. Now? Onto the technical jargon.
Rotating Bits
Nov 25th
Under normal circumstances, you can only ask for a bit rotation using assembly code, however, it is possible to imitate rotation through code.
First an explanation on rotating, and bits:
Goofy Sort
Nov 18th
You know it’s an easy algorithm to program when you’re done writing it, before the teacher is done explaining it…
b32b63e717156af2b77e4c3ed5c9f527019
DiceRoller v2
Apr 21st
I decided my first version wasn’t exactly flexible, so I rewrote it to parse out strings of dice rolls and then roll them.
/* * dice.c * * Created on: Apr 21, 2009 * Author: Mike Sherwood (coolhand2@gmail.com) * Modified on: Nov 18, 2009 * Author: Mike Sherwood (coolhand2@gmail.com) * Changes: Changed the if-series in modifySum * to be a select statement instead. */ #include <stdio .h> #include <time .h> #include <stdlib .h> int rollDice( int, int ); void modifySum( char, int, int* ); int main( int argc, char* argv[] ) { char* diceString = argv[1]; int amount, sides, modifier, sum; char mod; amount = sides = modifier = sum = 0; mod = '\0'; sscanf( diceString, "%dd%d%c%d", &amount, &sides, &mod, &modifier ); sum = rollDice( amount, sides ); printf( "Sum: %d\n", sum ); modifySum( mod, modifier, &sum ); printf( "Total: %d\n", sum ); return 0; } int rollDice( int amount, int sides ) { srand( time(NULL)/2 ); int i, value, sum; for( i = value = sum =0; i < amount; i++ ) { value = ( rand() % sides ) + 1; printf( "Dice %d: %d\n", i + 1, value ); sum += value; } return sum; } void modifySum( char mod, int modifier, int* sum ) { switch( mod ) { case '\0': printf( "No Modification\n" ); break; case '+': printf( "Add %d\n", modifier ); *sum += modifier; break; case '-': printf( "Subtract %d\n", modifier ); *sum -= modifier; break; case '*': case 'x': case 'X': printf( "Multiply %d\n", modifier ); *sum *= modifier; break; case '/': printf( "Divide %d\n", modifier ); *sum /= modifier; break; } return; }
Dice Roller v1
Apr 20th
A bit of C code I russelled up to roll a specific die based off the arguments passed to it through the command line. It’s basically just a random number generator with addition properties.
/* * dice.c * * Created on: Apr 20, 2009 * Author: Mike Sherwood <coolhand2 @gmail.com> */ #include <stdio .h> #include <stdlib .h> #include <time .h> int main( int argc, char* argv[] ) { int number, sides, i, val, sum; number = atoi( argv[1] ); sides = atoi( argv[2] ); val = sum = 0; srand( time( NULL )/2 ); printf( "Rolling: %dd%d\n", number, sides ); for( i = 0; i < number; i++ ) { val = (rand() % sides) + 1; printf( "Dice %d: %d\n", i+1, val); sum += val; } printf( "Sum: %d\n", sum ); return 0; }
ASM Words
Nov 13th
; ========================================================= ; Subroutine to check for a palindrome ; ; d0.w = length of string minus 1 ; a0.l = starting RAM address of string (must be 8 bits per character) ; returns with ZERO flag set if the string is a palindrome, clear otherwise ; ========================================================= CheckPalin: andi.l #$0000FFFF,d0 ;allow using d0 as long tst.w d0 ;is string 1 character long? beq.b CheckPalin_Return ;if so, return with ZERO flag set movea.l a0,a1 adda.l d0,a1 ;set a1 to address of last character subi.w #1,d0 ;get whole length lsr.w #1,d0 ;get half the length CheckPalin_Loop: move.b (a0),d1 ;get character from start of string cmp.b (a1),d1 ;compare with character from end of string bne.b CheckPalin_Return ;if unequal, return with ZERO flag unset adda.l #1,a0 ;increase offset from start of string suba.l #1,a1 ;decrease offset from end of string dbf d0,CheckPalin_Loop move.w #0,d0 ;set ZERO flag CheckPalin_Return: rts
The credit for this goes to Rob (keiji.msk@googlemail.com).
I may hate some (if not most) of his coding practices, but you gotta admit, a guy who can write a palindrome checker using 4 temporary variables, in ASM, is fucking smart, despite the answer being logical as hell.
C++ Pig Latinizer
Sep 28th
About three lines smaller actually. This version is only 94 lines, instead of the 97 lines of the php version… It confuses me, but eventually I’ll figure it out… I’m sure it has to do with amount of comments or something… Or the fact that it can only take one word…
Pig Latin Parser
Sep 27th
<?php /* * ======================================================================================== * File: Latinizer.php * Function: Take a string input and parse the word(s) into their respective pig-latin form * Author: Coolhand2 (Michael Sherwood) * Email: coolhand2@gmail.com * Copyright (c) 2006 by Coolhand2 * Lines of actual code: 35 * ======================================================================================== */ ?> <form action="<?php $_SERVER['PHP_SELF'] ?>" method="GET"> <input type="box" name="phrase" /> <input type="submit" value="Translate!" /> </form> <?php $vowelArray = array('a', 'e', 'i', 'o', 'u'); //Vowels to look for. //We don't wanna include these in the latinizing process, so we take these out... $delimiterArray = array('.' , ',' , '!' , ':' , ';' , '?', '~'); $endString = "ay"; //If it is latinized, we staple this on the end. $phrase = $_GET['phrase']; if( $phrase != '' ) { /* * =========================================================== * Get rid of any excess white space included in the phrase * Sperate the words into an array that we can iterate through * Iterate through each word. * =========================================================== */ $phrase = trim( $phrase ); $phraseArray = explode( " ", $phrase ); //Seperate the words through spaces. foreach( $phraseArray as $word ) { /* * ================================================= * Iterate through each word * So as to not get a result of "oot!shay" * get the last known position of a puncuation mark. * ================================================= */ $puncPos = strlen( $word ); while( in_array( $word{$puncPos}, $delimiterArray ) ) { $puncPos--; } /* * ======================================================================================== * Now we go through the word itself, and see if we can't find the first vowel in the word. * We use $i to keep track of the location of the mythical first vowel in the word * ======================================================================================== */ $i = 0; //Set $i to the first character position in any word. while( !in_array( $word{$i}, $vowelArray ) ) { $i++; } if( $i != $puncPos ) { /* * ================================================================ * Concatenate the string parts together * Segment of the word from the first vowel to the punctuation * Segment of the word from the beginning of the word to $i * "ay" * Then any punction there may be. * ================================================================ */ $string = ""; $string .= substr( $word, $i, ($puncPos - $i ) - 1); $string .= substr( $word, 0, $i ); $string .= $endString; $string .= substr( $word, $puncPos - 1 ); $newPhrase[] = $string; } else { $newPhrase[] = $word; } } /* * ======================================================= * When that's all said and done for each word, * we then put all the words back the way they used to be! * ======================================================= */ $string = implode( " ", $newPhrase ); echo $string; } else { echo "Input a string!"; } ?>