but I can't quite get right the flipping of the remaining 7 bits

I agree with dave_the_m that some more examples would be best. But to answer this part of the question, you can mask the value to only get the lower 7 bits with $val & 0x7F, and then flip the lower 7 bits with an XOR, i.e. $val ^ 0x7F, put together that's my $y = ($x & 0x7F)^0x7F;.

$z = $x & (1 << 7);

BTW, since you're probably just looking to get a true/false value from this, you don't need the bit shift: $x & 0x80 is enough to tell you if the high bit is set or not, since the return value will be either 0 (false) or 0x80 (true). Update: Actually, never mind - the compiler is of course smart enough to optimize (1 << 7) into 128. I personally prefer 0x80 or 0b1000_0000 over (1 << 7), which is why I tripped over this at first, but TIMTOWTDI.


In reply to Re: Flipping partial bits by haukex
in thread Flipping partial bits by rickss

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.