Linux HAM/Amateur Radio development
 help / color / mirror / Atom feed
* Slightly OT: Perl question
@ 2003-03-15 19:24 John Ackermann N8UR
  2003-03-15 22:46 ` Hamish Moffatt
  0 siblings, 1 reply; 3+ messages in thread
From: John Ackermann N8UR @ 2003-03-15 19:24 UTC (permalink / raw)
  To: linux-hams

Hi --

Sorry if this is a bit off-topic.

I need to do some bit-level manipulation in a perl program I'm writing to 
capture data from a frequency counter via GPIB.  I have dug around through 
the Camel Book and done some googling, but I haven't been able to find a 
straightforward way to take care of (what should be) straightforward tasks.

I need to:

(a) test whether one bit of a byte is set or not (e..g, is bit 5 of $myvar 
a 1 or a 0);

and

(b) do a binary AND of two bytes (in other words, apply a bitmask).

I'd sure appreciate a  perl snippet or two showing efficient ways to do 
these things.

Thanks,

John

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Slightly OT: Perl question
  2003-03-15 19:24 Slightly OT: Perl question John Ackermann N8UR
@ 2003-03-15 22:46 ` Hamish Moffatt
  2003-03-15 22:54   ` John Ackermann N8UR
  0 siblings, 1 reply; 3+ messages in thread
From: Hamish Moffatt @ 2003-03-15 22:46 UTC (permalink / raw)
  To: John Ackermann N8UR; +Cc: linux-hams

On Sat, Mar 15, 2003 at 02:24:47PM -0500, John Ackermann N8UR wrote:
> I need to:
> 
> (a) test whether one bit of a byte is set or not (e..g, is bit 5 of $myvar 
> a 1 or a 0);
> 
> and
> 
> (b) do a binary AND of two bytes (in other words, apply a bitmask).

Binary AND is the "&" operator as in C. So

$c = $a & $b;
$y = $x & 0xFF;

etc achieves (b), and similarly for (a),

if ($myvar & 0x20) {
    ....
}

or perhaps more readably:

if ($myvar & (1 << 5)) {
    ....
}


Hamish
-- 
Hamish Moffatt VK3SB <hamish@debian.org> <hamish@cloud.net.au>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Slightly OT: Perl question
  2003-03-15 22:46 ` Hamish Moffatt
@ 2003-03-15 22:54   ` John Ackermann N8UR
  0 siblings, 0 replies; 3+ messages in thread
From: John Ackermann N8UR @ 2003-03-15 22:54 UTC (permalink / raw)
  To: Hamish Moffatt; +Cc: linux-hams

Thanks, Hamish.  I don't know why it didn't work when I tried something 
similar to that before.  I'll give it another go.

73,
John

--On Sunday, March 16, 2003 09:46:04 +1100 Hamish Moffatt 
<hamish@cloud.net.au> wrote:

> On Sat, Mar 15, 2003 at 02:24:47PM -0500, John Ackermann N8UR wrote:
>> I need to:
>>
>> (a) test whether one bit of a byte is set or not (e..g, is bit 5 of
>> $myvar  a 1 or a 0);
>>
>> and
>>
>> (b) do a binary AND of two bytes (in other words, apply a bitmask).
>
> Binary AND is the "&" operator as in C. So
>
> $c = $a & $b;
> $y = $x & 0xFF;
>
> etc achieves (b), and similarly for (a),
>
> if ($myvar & 0x20) {
>     ....
> }
>
> or perhaps more readably:
>
> if ($myvar & (1 << 5)) {
>     ....
> }
>
>
> Hamish
> --
> Hamish Moffatt VK3SB <hamish@debian.org> <hamish@cloud.net.au>
>
>



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2003-03-15 22:54 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-03-15 19:24 Slightly OT: Perl question John Ackermann N8UR
2003-03-15 22:46 ` Hamish Moffatt
2003-03-15 22:54   ` John Ackermann N8UR

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox