http://qs321.pair.com?node_id=11114155


in reply to Re^2: Operate with bits
in thread Operate with bits

Thanks rizzo & soonix. Actually substr((sprintf "%b", $address), -7, 1) is serving my need as we are starting from right side of the bit stream (LSB) and starting to count from bit 0 to bit 5, so total of six bits and keeping -7 in the substr func as index makes us catch the 6th bit from the bit stream. Thanks again for the help guys

Replies are listed 'Best First'.
Re^4: Operate with bits
by AnomalousMonk (Archbishop) on Mar 12, 2020 at 07:24 UTC

    I don't understand why GrandFather's solution (bitwise-anding) would not give you the same result with a lot less wasted motion.


    Give a man a fish:  <%-{-{-{-<

      I assume that for most humans it is easier to see the "finding bit X" problem as one of dealing with strings, than as one of calculating (although, of course, the latter is much easier for the computer).

      In other words:

      sprintf '%b' converts to a string of bits, then I can take the 7th char from the right
      is easier to understand (for most humans) than
      If I have it as a number, I simply "AND" it with 26
      Of course, it all depends: if there are millions of lines, the inefficiency will add up and become noticeable.

        But we're not humans, we're programmers!


        Give a man a fish:  <%-{-{-{-<

      Maybe because syedasadali95 is actually dealing with hex digits (nibbles), not with bits as assumed by my post and stated by every one of syedasadali95's posts up until the reply to soonix.

      Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

        Even so:

        c:\@Work\Perl\monks>perl -wMstrict -le "LINE: for my $line ( 'tag:0x3bb5044d addr:0x1009db0 qospri:40 len:0x40', 'tag:0x3bb5043d addr:0x1009d40 qospri:30 len:0x30', 'tag:0x3bb5044d addr:0xb0 qospri:40 len:0x40', 'tag:0x3bb5043d addr:0x40 qospri:30 len:0x30', 'tag:0x3bb5044d qospri:40 len:0x40', 'tag:0x3bb5043d qospri:30 len:0x30', ) { my $got_addr = my ($hex_str) = $line =~ m{ addr:0x ([[:xdigit:]]+) }xms; ;; next LINE if not $got_addr; ;; my $bit_6_set = 0x40 & hex $hex_str; ;; printf qq{bit 6 %7s in %9s of '$line' \n}, $bit_6_set ? 'set' : 'not set', qq{'$hex_str'} ; } " bit 6 not set in '1009db0' of 'tag:0x3bb5044d addr:0x1009db0 qospri:40 + len:0x40' bit 6 set in '1009d40' of 'tag:0x3bb5043d addr:0x1009d40 qospri:30 + len:0x30' bit 6 not set in 'b0' of 'tag:0x3bb5044d addr:0xb0 qospri:40 len: +0x40' bit 6 set in '40' of 'tag:0x3bb5043d addr:0x40 qospri:30 len: +0x30'


        Give a man a fish:  <%-{-{-{-<