#!/usr/bin/perl use warnings; use strict; my $mask1 = 0b1101_1101; my $mask2 = 0b0010_0010; printf " \$mask1 = %b\n", $mask1; #Use bitwise NOT to flip all the bits. printf "~\$mask1 = %b\n\n", ~$mask1; #use bitwise OR to set bit(s) but keep original values for all other b +its. my $value = 0b1010_1000; printf " %b \$value\n| %b \$mask1\n----------\n %b\n\n\n", $value, $ +mask1, $value | $mask1; printf " %b \$value\n| %08b \$mask2\n----------\n %b\n\n\n", $value, + $mask2, $value | $mask2; #use bitwise AND to clear bit(s) but keep original values for all othe +r bits. printf " %b \$value\n& %b \$mask1\n----------\n %b\n\n\n", $value, $ +mask1, $value & $mask1; printf " %b \$value\n& %08b \$mask2\n----------\n %08b\n", $value, $ +mask2, $value & $mask2; #If you just want to test the value of a bit then clearing the other b +its # lets you test the value for the remaining one. __END__ $mask1 = 11011101 ~$mask1 = 111111111111111111111111111111111111111111111111111111110010 +0010 10101000 $value | 11011101 $mask1 ---------- 11111101 10101000 $value | 00100010 $mask2 ---------- 10101010 10101000 $value & 11011101 $mask1 ---------- 10001000 10101000 $value & 00100010 $mask2 ---------- 00100000

In reply to Re: Modifying multiple bits in a byte - bitwise operators by Lotus1
in thread Modifying multiple bits in a byte - bitwise operators by stevieb

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.