ODBiC Discussion Board

RE: Setting the nth bit of a variable, Nik Kamal, 11-03-2006

Need a new function

by Roger Harris, November 4, 2006 15:42

To set the nth bit, you need a number that only has the nth bit set. One way to get that is to raise 2 to the power on n. Unfortunately, I just realized that ODBscript doesn't support exponentiation! It's an oversight that no one has asked about before. When I was writing the expression analyzer, I had thought about supporting it as an operation character the way Basic does (e.g. 2^n), but then I decided to just use the C/C++ builtin pow(x,y) function. However, I neglected to include it in the list of math functions.

I have a version that supports that now. If you (or anyone else who has need of it) will please send me an email and let me know which version you have, I will send you a version with that function.

With that function, the expression would be: OR($var$, pow(2,$n$))

As a workaround, you could code a loop to get the number you need with the nth bit set:

IF $n$ = 0;
    SET bit = 1;
    SET bit = 2, cnt = 1;
    WHILE $cnt$ < $n$;
        SET bit = $bit$ * 2, cnt = $cnt$ + 1;

SET x = OR($var$, $bit$); %>


Post Your Reply:

E-mail  optional

HTTP Link: 
Link text: 


Copyright ©1997-2003, Roger Harris. All rights reserved.