Bitwise Operators

Bitwise operators are a very low level way to program and therefore not considered light reading, but lately I’ve been seeing enough interesting scenarios where they can be applied.

Since the basis for bitwise operators is to manipulate binary data, this is most suited for checking for 1 or 0.

Check to see if a number is odd ( 1 ) or even ( 0 ):


Use as a fast way to divide and floor:


get/set flags:


BITWISE OPERATOR LINKS

BINARY LINKS

»     » del.icio.us     » Digg It!

Tags: ,

No related posts

8 Comments »

  1. jgraup said,

    March 3, 2007 @ 6:23 pm

    http://pixeldepth.net/index.php?action=tutorials&category=1&id=1156254799

  2. jgraup said,

    March 3, 2007 @ 6:25 pm

    http://vijayram.wordpress.com/2007/01/30/actionscript-code-tricks-3/

  3. jgraup said,

    March 14, 2007 @ 8:00 pm

    Bitwise Color Creation
    http://www.flash-creations.com/notes/asclass_color.php

    example:
    // three decimal numbers, one each for R-G-B
    var nRed:Number = 101;
    var nGreen:Number = 153;
    var nBlue:Number = 204;
    cCircle.setRGB(nRed< <16 | nGreen<<8 | nBlue);

    see also
    http://www.flash-creations.com/notes/actionscript_operators.php

  4. jgraup said,

    March 14, 2007 @ 8:14 pm

    Simple way to get a gray color:

    function getRandomGrayscaleColor ( ) :Number
    {
    var brightness = Math.round (Math.random() * 255 );
    return getGrayscaleColor ( brightness );
    }

    function getGrayscaleColor ( brightness:Number )
    {
    return ( brightness <<16 | brightness <<8 | brightness )
    }

    for ( var i = 0; i < 100; i ++ )
    {
    // new ball
    var ball = this.attachMovie( ‘ball’, ‘ball_’ + i, this.getNextHighestDepth() );
    ball._x = Stage.width * Math.random();
    ball._y = Stage.width * Math.random();

    // set color
    var colorObj:Color = new Color ( ball );
    colorObj.setRGB ( getRandomGrayscaleColor ( ) );
    }

  5. jgraup said,

    March 16, 2007 @ 6:07 pm

    Bitwise XOR
    Bytes that differ are 1, bytes that are the same are 0. Converted to 32-bit binary integers before the operation occurs and the fractional portion of an operand, if any, is discarded.

    1 = 0
    0 = 1

    trace ( 1^1 ) // 0
    trace ( 0^1 ) // 1

  6. jgraup said,

    March 16, 2007 @ 6:18 pm

    One way to blindly toggle between a primary and secondary clip within an array.

    var nINX = ( clips [ 0 ].isPrimary ) ? 1 : 0;
    var oINX = ( nINX ^ 1 );

    var primaryClip = clips[ oINX ];
    var secondaryClip = clips[ nINX ];

    secondaryClip.isPrimary = !( primaryClip.isPrimary = true )

  7. jgraup said,

    April 15, 2007 @ 4:40 pm

    var a = false
    trace( a ) // false
    trace( a |= 0 ) // 0
    trace( a |= true ) // 1
    trace( a |= 99 ) // 99
    trace( a |= true ) // 99
    trace( a |= 0 ) // 99

  8. jgraup said,

    June 1, 2007 @ 1:19 am

    http://lab.polygonal.de/2007/05/10/bitwise-gems-fast-integer-math/

RSS feed for comments on this post · TrackBack URI

Leave a Comment

You must be logged in to post a comment.

best kansas city bookstore pirodr! 666