CFLib.org – Common Function Library Project

decBitwiseToQuery(decVal, colName)

Last updated November 3, 2005

Version: 1 | Requires: ColdFusion 5 | Library: UtilityLib

 
Rated 0 time(s). Average Rating: 0

Description:
You give me a number, and a column name. For each place in the resulting binary number, if that place evaluates out to a non-zero decimal number, that number will be added to the query as a row. If you gave me "13" for the number, you would get back a query with three rows; "1", "4", and "8".

Return Values:
Returns a query.

Example:

view plain print about
<cfset i = 13>
<cfset qryMyPizzaToppings = decBitwiseToQuery(i,"myChoice")>
<cfdump var="#qryMyPizzaToppings#">

Parameters:

Name Description Required
decVal Decimal value. Yes
colName Query column name to use. Yes

Full UDF Source:

view plain print about
<cfscript>
/**
 * You provide me with a decimal number, and a string for a column name and I will return to you a query containing of the decimal values for each resulting non-zero value when your number is converted to binary.
 * 
 * @param decVal      Decimal value. (Required)
 * @param colName      Query column name to use. (Required)
 * @return Returns a query. 
 * @author Alan McCollough (amccollough@anmc.org) 
 * @version 1, November 3, 2005 
 */

function decBitwiseToQuery(decVal,colName) {
    // create an empty counter
    var i = "";
    // create an empty 'current value'
    var cur = "";
    // convert decimal val to binary
    var bVal = FormatBaseN(val(decVal), 2);
    // set our binary seed to 1, the first place in the binary system
    var b = 1;
    
    // create a query object
    var qry = queryNew(colName);
    
    // loop through the places in the binary number, going from right to left.
    // Note, this is a brute-force method, and I'm sure some smart person out there
    // could figure out how to do this with pure bitwise functions. Me, I'm not that person.
    for(i = len(bVal); ; i = i - 1) {
            // set cur to the decimal value of the current binary place value
            cur = val(b * mid( bVal , i , 1 ));
            
            // If the result is greater than zero, add the result as a row to the query
            if (cur gt 0) {
                queryAddRow(qry);
                querySetCell(qry,colName,cur);
            };
                
            // double the value of our binary seed, for the next place if necessary
            b = 2 * b;
            
            //exit loop when the last place is processed
            if (i eq 1) break;
        }
    
    // return the query object
    return qry;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Dave Anderson Dave Anderson added
iniToStruct
20 day(s) ago

Dave Anderson Dave Anderson added
deDupeArray
20 day(s) ago

Richard Richard added
dice
22 day(s) ago

Isaac Dealey Isaac Dealey added
getRelative
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 22 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Raymond Camden highlightAndCrop
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson