decBitwiseToQuery(decVal, colName)
Last updated November 03, 2005
Version: 1 | Requires: CF5 | Library: UtilityLib
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:
<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:
/**
* 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;
}
Search CFLib.org
Latest Additions
Raymond Camden added
QueryDeleteRows
November 04, 2017
Leigh added
nullPad
May 11, 2016
Raymond Camden added
stripHTML
May 10, 2016
Kevin Cotton added
date2ExcelDate
May 05, 2016
Raymond Camden added
CapFirst
April 25, 2016