Visualizing Binary Numbers

Actionscript:
  1. [SWF(width=320,height=512,backgroundColor=0x000000,frameRate=30)]
  2.  
  3. var canvas:BitmapData=new BitmapData(32,512,false,0xFFFFFF);
  4. addChild(new Bitmap(canvas)).scaleX = 10;
  5.  
  6. var a:uint ;
  7. var s:String;
  8. var m:Number = 0;
  9. var d:Number = 0;
  10. var mi:int ;
  11. var r:Number = 0xFFFFFF / stage.stageWidth;
  12.  
  13. addEventListener(Event.ENTER_FRAME, onLoop);
  14.  
  15. function onLoop(evt:Event):void {
  16.    
  17.     d = mouseX * r;
  18.     m += (d - m) / 30;
  19.    
  20.     mi = int(m);
  21.  
  22.     canvas.lock();
  23.     canvas.fillRect(canvas.rect, 0xFFFFFF);
  24.  
  25.     a = 0xFFFFFFFF;
  26.     for (var i:int = 0; i<512; i++) {
  27.         s = (a -= mi).toString(2);
  28.         for (var j:int = 0; j<s.length; j++) {
  29.             if (s.charAt(j)=="0") {
  30.                 canvas.setPixel(j, i, 0x000000);
  31.             }
  32.         }
  33.     }
  34.     canvas.unlock();
  35. }

The above uses setPixel() to visualize numbers in binary format. You can move your mouse left and right to change an incremental counting value....

Here's a still generated by this snippet:

This entry was posted in BitmapData, misc, pixel manipulation and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

2 Comments

  1. Posted February 6, 2009 at 12:27 am | Permalink

    Hi
    Inspired by this here’s my study with decimals 1/2, 1/3 , 1/4 etc … in binary:
    http://www.flickr.com/photos/pixelero/3256475254/

  2. Posted February 6, 2009 at 8:58 am | Permalink

    very nice pixelero…

One Trackback

  1. By Weekly Shared Items - 6. February, 2009 | toxin 2.0 on February 5, 2009 at 10:45 pm

    [...] Visualizing Binary Numbers [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*