– Common Function Library Project

decBitwiseToQuery(decVal, colName)

Last updated November 03, 2005


Alan McCollough

Version: 1 | Requires: CF5 | Library: UtilityLib

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.


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


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

Full UDF Source:

 * 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 ( 
 * @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) {
            // 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;


Latest Additions

Raymond Camden added
November 04, 2017

Leigh added
May 11, 2016

Raymond Camden added
May 10, 2016

Kevin Cotton added
May 05, 2016

Raymond Camden added
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson