Home > Archive > AWK > November 2004 > Xor
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| KosayaC@netscape.net 2004-11-22, 8:55 pm |
| Is ther a way to do AND, exclusive OR, and inclusive OR bitwise assignments in
awk. I don't mean gawk.
Thank you
| |
| Bob Harris 2004-11-22, 8:55 pm |
| In article
<e3tod.955884$Gx4.460779@bgtnsc04-news.ops.worldnet.att.net>,
KosayaC@netscape.net wrote:
> Is ther a way to do AND, exclusive OR, and inclusive OR bitwise assignments
> in
> awk. I don't mean gawk.
>
>
> Thank you
No. At least not on my Tru64 UNIX workstation.
My best guess is that awk as written by Aho, Weinberger, Kernighan used
floating point variables for all numbers. Floating point does not lend
itself well to Bitwise AND, Bitwise Inclusive OR, and Bitwise Exclusive
OR. But that is just a guess.
Bob Harris
| |
| Don Stokes 2004-11-23, 3:55 am |
| In article <e3tod.955884$Gx4.460779@bgtnsc04-news.ops.worldnet.att.net>,
<KosayaC@netscape.net> wrote:
>Is ther a way to do AND, exclusive OR, and inclusive OR bitwise assignments in
>awk. I don't mean gawk.
Not as simple expressions (a la C's |, &, ~ and ^ operators), which is
why gawk has the bitwise functions.
But depending on what you're trying to achieve, you can do it by
arithmetic means. Single bits (1 or 0) can use normal logical operators
(&&, ||, !), shifts can be done by multiplication, and the combination
of the two can be used to achieve bitwise operations. % and int() deal
with trimming down to one bit.
For example, a generalised 32 bit bitwise AND can be implemented as:
function OR(a, b,
r, i) {
r = 0
for(i = 0; i < 32; i++)
r += ((int(a / 2^i) % 2) && (int(b / 2^i) % 2)) * 2^i
return r
}
(Yes, it could be much more efficient, I'm just illustrating the method.)
and OR just needs to replace the && operator with a ||. Exclusive OR
needs a little more work, but not much.
However, you can be much more efficient if you know what you're doing.
For example, if you're masking bit ranges you can be more clever -- I do
quite a bit of IP address manipulation without using bitwise functions at
all. For example:
a & 0xffffff00 -> int(a / 256) * 256
a & 0x000000ff -> a % 256
a & 0x00ff0000 -> (int(a / 65536) % 256) * 65536
(a & 0xffffff00) | (b & 0x000000ff)
-> (int(a / 256) * 256) + (a % 256)
Note that the last expression uses the + instead of the |, as there is
no danger of the bits colliding.
-- don
| |
| William James 2004-11-23, 8:55 am |
| Bob Harris <harris@zk3.dec.com> wrote in message news:<harris-7CBC64.17435322112004@cacnews.cac.cpqcorp.net>...
> In article
> <e3tod.955884$Gx4.460779@bgtnsc04-news.ops.worldnet.att.net>,
> KosayaC@netscape.net wrote:
>
>
> No. At least not on my Tru64 UNIX workstation.
>
> My best guess is that awk as written by Aho, Weinberger, Kernighan used
> floating point variables for all numbers. Floating point does not lend
> itself well to Bitwise AND, Bitwise Inclusive OR, and Bitwise Exclusive
> OR. But that is just a guess.
>
> Bob Harris
Sometimes bitwise operations aren't needed. For example, instead of
bit_and( x, 2^n-1 ) you can do x % 2^n.
If you need bitwise AND and you can stand the slow speed, use this:
function bit_and( m, n, val,result )
{ val = 1 ; result = 0
while ( m || n )
{ if ( m % 2 && n % 2 )
result += val
val += val
m = int(m/2) ; n = int(n/2)
}
return result
}
|
|
|
|
|