[Asterisk-Dev] "true" is -1 or 1 in boolean functions ?
Luigi Rizzo
rizzo at icir.org
Tue Jan 17 01:03:57 MST 2006
On Mon, Jan 16, 2006 at 01:38:22PM -0600, Tilghman Lesher wrote:
> On Monday 16 January 2006 04:59, Luigi Rizzo wrote:
> > i notice some inconsistency in the return values for
> > functions that have a boolean result - some pieces of code
> > use -1, others use 1.
> >
> > I am totally neutral on the topic, but would like if someone
> > could put a note in the CODING-GUIDELINES on which one is
> > the recommended method, because mixing the two things is
> > confusing and error prone, as:
> >
> > - people looking at the code might think that the difference
> > means something other than the habit of the original coder;
> >
> > - people using these functions might do odd things such
> > as a bitwise AND on the result of such functions and other
> > operands, which has different results depending on whether -1
> > or 1 is used (i think i have seen such instances in the code,
> > sometimes it is convenient, althoguh obfuscating);
>
> Please show me how you can bitwise AND these two numbers and
> come up with zero. The only thing that matters is that the result is
> always non-zero. There simply isn't an error that can be generated
> by mixing these two numbers.
there are several error scenarios - i said "such as a bitwise AND"
because i did not want to enumerate all of them. Anyways, here
are two:
- The XOR of two booleans fails if they can be either +1 or -1.
To make it work you have to wrap the results into functions
that generate uniform values. No big deal, but it can be easily
forgotten and it clutters the code.
- [bitwise and] if you know that 'in asterisk, true is -1' you may be
tempted to write things like
if ( (res = (ast_waitdigit(c...) & is_this_true(x...)) ) )
where the latter is returning true or false, the former is not,
but the masking suffices, and if the specification of "true"
is -1 the construct is correct. I wouldn't use such a construct
myself, but sometimes might be a convenient shorthand.
With a mixed +1/-1 environment, the above fails.
cheers
luigi
More information about the asterisk-dev
mailing list