If you want work with a byte to contain your values in the range 0 - 255 then you should use pack rather than just assigning the value. Here's a script that uses subroutines to flip, set or unset bits within a byte, bits range from 7 as most significant to 0 as least. Each routine uses vec to create a mask of the bits to be manipulated and displays the mask to show what is going on.

use strict; use warnings; use feature qw{say}; my $base = 128; say q{Non-packed base - }, unpack q{B*}, $base; say q{}; $base = pack q{C}, 128; say q{Packed base - }, unpack q{B*}, $base; my $three = pack q{C}, 3; say q{Three for or - }, unpack q{B*}, $three; $base |= $three; say q{Base now - }, unpack q{B*}, $base; say q{}; $base = pack q{C}, 138; say q{Revised base 138 - }, unpack q{B*}, $base; flipBits( \ $base, 0 .. 2 ); say q{Flipped base 141 - }, unpack q{B*}, $base; say q{}; unsetBits( \ $base, 0 .. 7 ); say q{Cleared base - }, unpack q{B*}, $base; say q{}; setBits( \ $base, 7 ); say q{Set high bit - }, unpack q{B*}, $base; say q{}; setBits( \ $base, 0, 1 ); say q{Set two lowest - }, unpack q{B*}, $base; say q{}; flipBits( \ $base, 0 .. 3 ); say q{Flip four lowest - }, unpack q{B*}, $base; say q{}; setBits( \ $base, 0 .. 2 ); say q{Set three lowest - }, unpack q{B*}, $base; say q{}; sub flipBits { my( $rsByte, @bits ) = @_; my $vec; vec( $vec, $_, 1 ) = 1 for @bits; say q{Flip mask - }, unpack q{B*}, $vec; ${ $rsByte } = ${ $rsByte } ^ $vec; } sub setBits { my( $rsByte, @bits ) = @_; my $vec; vec( $vec, $_, 1 ) = 1 for @bits; say q{Set mask - }, unpack q{B*}, $vec; ${ $rsByte } = ${ $rsByte } | $vec; } sub unsetBits { my( $rsByte, @bits ) = @_; my $vec; vec( $vec, $_, 1 ) = 1 for @bits; say q{Unset mask - }, unpack q{B*}, $vec; ${ $rsByte } = ${ $rsByte } & ( ~ $vec ); }

The output.

Non-packed base - 001100010011001000111000 Packed base - 10000000 Three for or - 00000011 Base now - 10000011 Revised base 138 - 10001010 Flip mask - 00000111 Flipped base 141 - 10001101 Unset mask - 11111111 Cleared base - 00000000 Set mask - 10000000 Set high bit - 10000000 Set mask - 00000011 Set two lowest - 10000011 Flip mask - 00001111 Flip four lowest - 10001100 Set mask - 00000111 Set three lowest - 10001111

I hope this is helpful.

Cheers,

JohnGG


In reply to Re: Modifying multiple bits in a byte - bitwise operators by johngg
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.