Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Xor
Is ther a way to do AND, exclusive OR, and inclusive OR bitwise assignments 
in
awk.  I don't mean gawk.


Thank you

Report this thread to moderator Post Follow-up to this message
Old Post
KosayaC@netscape.net
11-23-04 01:55 AM


Re: Xor
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 assignment
s
> 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

Report this thread to moderator Post Follow-up to this message
Old Post
Bob Harris
11-23-04 01:55 AM


Re: Xor
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

Report this thread to moderator Post Follow-up to this message
Old Post
Don Stokes
11-23-04 08:55 AM


Re: Xor
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
}

Report this thread to moderator Post Follow-up to this message
Old Post
William James
11-23-04 01:55 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:27 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.