[petsc-dev] technical C question

Matthew Knepley knepley at gmail.com
Thu Apr 7 14:32:49 CDT 2016


On Thu, Apr 7, 2016 at 2:28 PM, Barry Smith <bsmith at mcs.anl.gov> wrote:

>
>    We have code
>
>   return (BT_idx        = index/PETSC_BITS_PER_BYTE,
>           BT_c          = array[BT_idx],
>           BT_mask       = (char)(1 << index%PETSC_BITS_PER_BYTE),
>           array[BT_idx] = (char)(BT_c | BT_mask),
>           (char)(BT_c & BT_mask));
>
> inside
>
> petscbt.h
>
> the clang static code analyzer reports
>
> In file included from
> /sandbox/petsc/petsc.clone/include/petsc/private/matimpl.h:1233:
> /sandbox/petsc/petsc.clone/include/petscbt.h:85:36: warning: The result of
> the '<<' expression is undefined
>           BT_mask       = (char)(1 << index%PETSC_BITS_PER_BYTE),
>                                  ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> Is the code wrong/unnecessary or clang?
>

It looks right to me:

  index%PETSC_BITS_PER_BYTE

gives the offset of this bit in the current byte.

  1 << index%PETSC_BITS_PER_BYTE

Gives an int with a 1 at the offset'th bit, and then
we cast to a char.

I have no idea why clang complains.

    Matt


>    Thanks
>
>


-- 
What most experimenters take for granted before they begin their
experiments is infinitely more interesting than any results to which their
experiments lead.
-- Norbert Wiener
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-dev/attachments/20160407/e2df12f3/attachment.html>


More information about the petsc-dev mailing list