The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to access values of awk/nawk variables outside the awk/nawk block? saniya Shell Programming and Scripting 5 05-13-2008 07:37 AM
nawk/ksh help DeltaX Shell Programming and Scripting 0 03-06-2008 03:54 PM
NAWK question DeltaX Shell Programming and Scripting 6 02-01-2008 06:34 PM
nawk -v to awk kamel.seg Shell Programming and Scripting 2 12-18-2007 07:30 AM
nawk whatisthis Shell Programming and Scripting 3 09-29-2004 01:44 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-04-2008
DeltaX DeltaX is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 16
nawk question

I am trying to write the following c code in nawk:

while ((ch = getc (fp)) != EOF)
{
total_bytes++;
checksum = (checksum >> 1) + ((checksum & 1) << 15);
checksum += ch;
checksum &= 0xffff; /* Keep it within bounds. */
}

I appreciate your help
  #2 (permalink)  
Old 03-04-2008
jim mcnamara jim mcnamara is online now Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,738
My nawk man pages do not have and "&=" or ">> <<" shift left or shift right operators.
nawk apparently does not support that. You can fake >> and << with division by 2 and multiplication by two. I don't know a way to fake the "and" operation without writing
some very tricky code. You are doing 32 bit operations. That means "anding" 32 values.

Try cksum or write your own C code. That makes more sense.

Maybe one of our ultimo awkers have done this solidly, but it looks like a nightmare waiting to happen to me.
  #3 (permalink)  
Old 03-04-2008
DeltaX DeltaX is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 16
Thanks for your prompt response. We will see if someone can help

Thanks again
  #4 (permalink)  
Old 03-04-2008
DeltaX DeltaX is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 16
yea my problem is how to do the AND.
  #5 (permalink)  
Old 03-04-2008
jim mcnamara jim mcnamara is online now Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,738
Here is your problem, why no "&" operator:

really old awk implementations used single precision floating point for numbers.
Newer awks, like nawk, use double precision. None use 32 bit integers.
  #6 (permalink)  
Old 03-04-2008
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,121
the most recent 'gawk' supports bitwise ops: and, or, xor, compl, lshift and rshift
here's something found seaching comp.lang.awk:
Code:
function awkand(a,b,  k,r) {
  k = 1
  r = 0
  a = int(a + 0.5) # round to nearest integer
  b = int(b + 0.5)
  while ( a + b ) {
    if ( a%2 && b%2 ) r += k
    a = int(a / 2)
    b = int(b / 2)
    k *= 2
  }
  return r

}
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:11 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0