Stand by...

Bitwise Operations: Part 2

Starting
So in order to be able to work with bits, we first need to recognize we will be doing bit level operations in binary, but it must be known that we can use hex code to make our lives much easier.

Do recognize that the following are identical in value and just differentiate in notation:

BINARY	HEXDECIMAL	DECIMAL
0001		0x01		1
0010		0x02		2
0011		0x03		2
1010		0x0A		10
1100		0x0C		12
1111		0x0F		15

(While reading this it will be tempting to ask, why is this important, or how can this be useful, and I assure you these topics will be covered in the next article.)

Links to Useful Reading
If you are not entirely comfortable with hex or binary yet, take a moment to do a bit more reading on either of these two topics;

Wikipedia on Binary:http://en.wikipedia.org/wiki/Binary_numeral_system
Wikipedia on Hexdecimal: http://en.wikipedia.org/wiki/Hexadecimal

Bitwise NOT
The Bitwise NOT inverses the binary digits, any bits that are off are turned on, and any bits that are on are turned off.
The Bitwise NOT operator (in PHP) is the Tilde(~).

	~1101	(yields) 0010
	~0000 (yields) 1111
	~1010 (yields) 0101

Bitwise AND
The Bitwise AND operator compares two binary series and creates a resulting series. The resulting series will only have ON(1) bits where both values in that position were on.

		0101
	AND	1001
	-------------
	END	0001

As you can see above the end result is a series that has on bits only where bits in the same position were on, in this case the last position (left to right).

heres a couple more examples;

		0101			1111
	AND	1101		AND	1010
	-------------		------------
	END	0101		END	1010

Bitwise OR(inclusive OR)
The Bitwise OR operator compares two binary series and creates a resulting series. The resulting series will have ON(1) bits where either value or both values in that position were on.

		0101
	OR	1001
	-------------
	END	1101

From the example above you can see that the “inclusive or” looks for any bits that are on and places an on bit in that position for the end result. Heres a couple more exmples.

		0101			1111
	OR	1101		OR	1010
	-------------		------------
	END	1101		END	1111

Bitwise xOR(exclusive OR)
The Bitwise OR operator compares two binary series and creates a resulting series. The resulting series will have ON(1) bits where either value, but NOT both values in that position is on.

		0101
	xOR	1001
	-------------
	END	1100

From the example above you can see that the “exclusive or” looks for any bits that are on and places an on bit in that position for the end result, aslong as both bits are not ON in that same position. Heres a couple more exmples.

		0101			1111
	xOR	1101		xOR	1010
	-------------		------------
	END	1000		END	0101

Bitwise SHIFT
Using two greater than or less than signs concurrently, we can shift the digits in the binary number to the left or righ by a specified amount.

LEFT SHIFT			RIGHT SHIFT
------------------		------------------
	0011 << 2			1100 >> 2
END	1100			END	0011

What happens when a shift occurs is all the digits are litteraly shifted to a direction, and zeroes are put in as fillers to the left or the right.

Signs
We have not begun to use the Bitwise operators in our examples in order to try and convey the meaning behind them a bit better.
Bitwise Operations use their own set of operators which include the following, and it should be noted that these operators are very common, but you should look up bitwise operators for your language of choice.

&	BITWISE AND
|	BITWISE OR
^	BITWISE xOR
~	BITWISE NOT
>>	BITWISE SHIFT RIGHT
<<	BITWISE SHIFT LEFT

*Note, these operators night be different depending on language specifications, the ones shown here are primarily taken from PHP, as that we be the language used to provide an example in later articles.

NextKeep in mind what these operators do, next Article we will go over how their interaction affects the result, the purpose of this was to introduce you into how these operators work on the binary numbers.

Bitwise Operations: Part 1

What
First lets begin by establishing the meaning of bitwise;

Bitwise: Being an operation that treats a value as a series of bits rather than a numerical quantity.

And what this means is that we will be dealing with bits in the binary numeral system, so in essence we will be dealing with sequences of zeroes(0) and ones(1).

Why
Bitwise Operations work at the binary level and can have memory saving consequences when used properly. They also make storing a vast number of boolean value in one number possible, which can lead to a better designed system.

Signs
Bitwise Operations use their own set of operators which include the following;

&	BITWISE AND
|	BITWISE OR
^	BITWISE xOR
~	BITWISE NOT
>>	BITWISE SHIFT RIGHT
<<	BITWISE SHIFT LEFT

*Note, these operators night be different depending on language specifications, the ones shown here are primarily taken from PHP, as that we be the language used to provide an example in later articles.

Simple Lightning RAW AS3

This was a small project to see what how I could possibly create an “electricity” effect…This AS3 is still pretty raw , use it to enhance and get an idea of how to implement this…its a start….
SEE IT HERE: http://www.myflexria.com/experiments/xoxo/LightningTest.html

package
{
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.filters.GlowFilter;
 
	[SWF(backgroundColor=0xFFFFFF)]
	public class LightningTest extends Sprite
	{
		private var cnt:int = 0;
		private var obj:Array = new Array();
		public function LightningTest()
		{
			super();
			stage.scaleMode = StageScaleMode.NO_SCALE;
			this.addEventListener(Event.ENTER_FRAME, CreateLightning);
		}
 
 
		private function CreateLightning(e:Event):void
		{
			var lines:Shape = new Shape();
			lines.graphics.lineStyle(.5,0xFFFFFF); //thickness & color of electricity
 
			var count:int = 0;
			var oldRand:int;
			var oldRand2:int;
			while(count < 50) //50 refers to the number of steps in the lightning strike.... the more the longer...
			{
				var rand1:int = oldRand+Math.random()*6; //6 is the length of the x potion of segment in the ray
				var rand2:int = oldRand2+Math.random()*6; //6 is the length of the y potion of segment in the ray
 
				lines.graphics.lineTo(oldRand+rand1,oldRand2+rand2);
 
				count++;
 
				oldRand = rand1;
				oldRand2 = rand2;
			}
 
			var glow:GlowFilter = new GlowFilter();
			glow.color = 0x0099FF;
			glow.alpha = .25;
			glow.blurX = 5;
			glow.blurY = 5;
 
			lines.filters = [glow];
			this.addChild(lines);
 
			cnt++;
			if(cnt == 10) //10 is the number of electricity lines on the stage at any given time...
				this.addEventListener(Event.ENTER_FRAME, RemoveLightning); //will remove the oldest object
		}
 
		private function RemoveLightning(e:Event):void
		{
				removeChildAt(0);
		}
	}
}

PHP SimpleXML RSS Snippet

Snippet for reading an RSS Feed…Simple, you can hopefully build on it to make it fulfill your purpose.

<?php
//path to the rss feed, can be a local file or a URL
$path = 'rss.xml';
 
//get the files contents
$read = file_get_contents($path);
 
//casting it as SimpleXML object (PHP Built-in Method)
$myXML = simplexml_load_string($read);
 
$i = 0;
foreach ($myXML->channel->item as $item) {
	//the following items are required for a valid RSS 2.0 Feed
	$myInfo[$i]['link'] = (string)$item->link;
	$myInfo[$i]['title'] = (string)$item->title ;
	$myInfo[$i]['description'] = (string)$item->description ;
 
	//if you have additional tags you would like to retrieve, use the tag name like so...
	$myInfo[$i]['pubDate'] = (string)$item->pubDate ;
	$i++;
}
//now you have all the information available in $myInfo at your disposal.
print_r($myInfo);
?>

Actionscript3 Compiler & Runtime Errors

On this webpage you can find a list of some AS3 Compile/Runtime Errors, Warnings. Its not a full list by any means, although I do feel it can provide some great suppourt in troubleshooting your application.

Link:

Compiler Errors - http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/compilerErrors.html

Compiler Warnings - http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/compilerWarnings.html

Runtime Errors - http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/runtimeErrors.html

C# Variable & Methods

Methods inside of a class are composed of two parts.
Method Header:
(Method Return Type) (Method Name)([Method Parameters[,...]])
eg:

  void MyMethod( int myParam1, string myParam2)

Method Body:
Enclosed in curly braces, it may contain invocations to other methods, variables and statements, and other blocks embedded within.
eg:

{
  int localVar = 0;
  Console.WriteLine("This is a Line");
  while(localVar &lt; 5)
  {
	  Console.WriteLine("This is a Line In a Loop");
	  localVar++;
  }
}

Variables, Constants & Fields inside Methods.

                        |	Instance Field	        |		Local Variable		|		Local Constant
 
Lifetime		|Starts when the instance       |Starts at the point in		        |Same as local variable
                        |is created.			|the block where it is 		        |
                        |Ends when the instance is	|declared. Ends when the end            |
                        |no longer accessible		| of the block is reached.	        |
 
Implicit 		|Initialized to a default 	|No implicit initialization	        |Must declare value on
Initialization	        |value; 0, null, false.		|A value must be assigned	        |initialization!
                        |				|before use.		                |
Storage Are		|Heap, regardless of type	|Value Type: Stack			|
							|Reference Type: Ref in Stack
							|data stored in heap	          	|
 
keyword			|Must specify type		|"var"Can only be used when             |"const" must be used before
|							|the variable is initialized            |the type is declared.
|							|The type will be infered 	        |Can not be changed after
|							|and can not be changed		        |initialization.

Return Types

Earlier, we specified that we must declare a return type for the methods we declare.

We can specify a regular type such as, int, string, float, double, char ect. or we can declare a return type of a user-defined object.

Within the method we would use the keyword “return” to provide the result of the Method.

class MyExample{
	int MyCalculations()
	{ 			//Method MyCalculations expected to return an int
		int myMagicNumber = 1000;	//just a local variable
 
		return myMagicNumber;		//return exits the Method and provides the value of myMagicNumber (1000);
	}
 
	void Main()
	{
		int storeMyCalculations = MyCalculations();		//invoking my method, and assigning it returned value to a local variable
	}
 
}

If a method has a void return type then the return can be used to exit the code block early.

void updateSomething() //void return type specified
{
	if(/*conditional statement here*/)
		return;	//this will cause us to exit the method, to the point of invokation
	else
		/*do something here*/
}

Parameters
Methods can specify parameters

The Formal Parameters(expected by method) are;
specified within the prethesis
separated by comma
must specify a type and an identifier
the identifier is used for use whithin the mothod.

int/*will return an int*/ updateSomething(int numOne/*(TYPE) (LOCAL IDENTIFIER),...*/, int numTwo, string words)
{
	if(numOne &lt; numTwo)
		return numTwo; //return an int
	else
		return numOne; //return an int
}

actual parameters are what we will use to plug in.

updateSomething(5, 1, "some words"); //this method is being invoked with actual variables in for the expected parameters
 
int one = 5;
int two = 6;
string someWords = "Hello";
 
updateSomething(one, two, someWords); //using local variables for actual parameters.

Things to keep in mind about parameters is that the number of actual parametes must match the number of expected formal parameters, and the types need to match also.

eg, if the formal parameter is an int, you can not use a string for that particular parameter.

Some C# Basics

Comments

/*This Is
A Block
Comment */
 
//This is a Line Comment
 
///<description>
///XML based meta comment
///This is a meta-data comment
///</description>

Classes
Classes are reference types, means they require memory for both the references to the data and the actual data(stack vs heap)
A class is a blueprint for an object, and object that will be a field….so at some point we will have a field with type MyNewClass if we go with the code below.

class MyNewClass
{
//Class Body Goes Here
}

a field is a variable that belongs to a class.
It can be of any type, predefined or user-defined.
They can be read from and written to.
A value must be assigned or the variable must be initialized at compile time.
If the field is not initialized then it is set to a default value; 0, null, or false.

class MyNew Class{
     int/*Type*/ myRomanNumeral/*Identifier*/; //type followed by identifier
     int theCount;                    //initialized to 0;
     string someWords ;                //initialized to null;
 
     int myCount = 100;             //initialized to 100
     string myWords = "abcdxyz";    //initialized to "abcdxyz"
 
     //declare multiple fields of the same type
     /*You can not declare multiple types within one line.*/
     int countOne, countTwo = 6, CountThree;
}

Methods
Have a return type, Name, Parameter List, Method Body.

class MySimpleClass{
   void PrintThis(string myPrintString)  //(Return Type)  (Name)(((Type) (ParameterIdentifier))[,...])
    {
    Console.WriteLine(myPrintString);
    }
}

Just Some Basics

TextField with buttonMode

One of the more frustrating things about working with text has to be the fact that you are unable to set buttonMode on a TextField by itself.

I have seen many place on internet suggesting that I use the following code

var myParentMC:MovieClip = new MovieClip();
var myChildTF:TextField = new TextField();
 
myParentMC.buttonMode = true;
myParentMC.mouseChildren = false;
 
myParentMC.addChild(myChildTF);

whilst this is great it adds a tad too much code if I have many links…

I have thus written a class to be able to recreate this effect a with a bit less code when I decide I need to make a linkable text field and I want the hand cursor on it.

Heres my class:

package com.myFlexRIA.text  //You may need to make an adjustment to an appropriate Package
{
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFormat;
 
	public class TextLink extends Sprite
	{
		private var textFieldArea:TextField;
		public function TextLink()
		{
			super();
			this.mouseChildren = false;				//Disables Individual Children
			this.buttonMode = true;  				// enables hand cursor on mouse Over
 
			textFieldArea = new TextField();
			addChild(textFieldArea);
		}
 
		/*
		The following function sets the different attributes for the textArea.
		Use the following to set the properties of the textfield.
		**
			TextLink.textAreAttributes = {text:'TextArea Text', color:0xFFFFFF};
		**
		*/
		public function set textAreaAttributes(object:Object):void
		{
			for (var property:String in object)
			{
			    textFieldArea[property] = object[property];
			}
		}
 
 
		/*
		The following function allows you to set a the TextFormat Directly.
		*/
		public function set setDefaultFormat(object:TextFormat):void
		{
			textFieldArea.defaultTextFormat = object;
		}
 
		/*
		The following function allows you to get a the text directly.
		*/
		public function get text():String
		{
			return textFieldArea.text;
		}
 
		/*
		The following function allows you to set a the text directly.
		*/
		public function set text(value:String):void
		{
			textFieldArea.text = value;
		}
 
	}
}

and this is how you would go about using the class

var myLink:TextLink = new TextLink();
 
myLink.text = 'Set My Text Directly';//You can access the text property of the textfield directly
myLink.textAreaAttributes = {text:'This Was Set Using An Object',antiAliasType: AntiAliasType.ADVANCED, alpha:.75};//use an object to set other properties of the text area
 
addChild(myLink);
 
//extra settings
myLink.setDefaultFormat = new TextFormat(null,null,0x003366,true);//set a new TextFormat for styling

Keep in mind; This is by no mean a complete solution, I added this in hope that many of you will be able to branch off this and create your own class and add your desired functionality. You could very well add a width and height get/set method so that you can have more control over the view. Possibly you could add a method that adds Event Listeners so that you can define an URL string to go when it is clicked…There is no limit what can be done. Use this code, modify it as you will, and share it!

Cheers!

C# and Silverlight Anyone?

So after much thinking, I have decided to dip into the world of Microsoft and learn C# and test out the waters with Silverlight. Although Flash’s Actionscript will still be my preferred platform and language.

For starters, I bought a book to learn C# from, and will be learning more about Silverlight as I go through the tutorials on Silverlight’s website. http://silverlight.net/GetStarted/

Many articles in the news presently point to Silverlight being in trouble and not performing to expectation, but I feel that it might be too early to tell, and the rate of development from 2.0 to 3.0 has been rather quick also. Since I am very curious about this I will also include some updates on my Silverlight experiences.

Some Related Articles to Silverlight:
http://www.internetnews.com/webcontent/article.php/3822781/Microsoft+Silverlight+3+to+Shine+on+July+10.htm
http://www.crn.com/software/217700870;jsessionid=BVIE0HHDEKVTYQSNDLPSKH0CJUNN2JVN

private var body:Shape = new Shape();

Over the past couple of weeks, I have decided to become more active in the terms of lifestyle I live. I have begun to exercise with greater frequency and in turn I have noticed I feel sharper and more concentrated when I am writing my applications, whether it be in PHP, ActionScript, Javascript, or simple HTML & CSS. The solutions feel more obvious and everything has a better flow.

I would definitely suggest that what ever it is you do for a living, I recommend exercise to sharpen the body and refresh the mind.

Cheers!