I'm completely new to awk, so don't even know if this is possible. I
need to modify some old code so that it performs a bitwise "and" on
two numbers. Doing something like "variable = 7 AND 9" returns 79
rather than 1.
Please help.......
Rich
|
|
0
|
|
|
|
Reply
|
richdyer_2000 (3)
|
12/4/2003 10:45:30 AM |
|
richdyer_2000@hotmail.com (Richard Dyer) writes:
> I'm completely new to awk, so don't even know if this is possible. I
> need to modify some old code so that it performs a bitwise "and" on
> two numbers. Doing something like "variable = 7 AND 9" returns 79
> rather than 1.
There's no built-in binary AND in standard awk, you'll have to do it
the hard way with a division loop, or call an external program.
Here's an example of a division loop ANDer (not optimized in
any way and slow as molasses, but it should work):
function binand(x,y,
z,n) { # local variables
z = 0
n = 1
while (x >= 1 && y >= 1) {
if (x%2 == 1 && y%2 == 1) z += n
x = (x - (x%2))/2
y = (y - (y%2))/2
n *= 2
}
return z
}
--
Tapani Tarvainen
|
|
0
|
|
|
|
Reply
|
Tapani
|
12/4/2003 11:36:57 AM
|
|
In article <12652ad6.0312040245.5b2221ab@posting.google.com>,
Richard Dyer <richdyer_2000@hotmail.com> wrote:
>I'm completely new to awk, so don't even know if this is possible. I
>need to modify some old code so that it performs a bitwise "and" on
>two numbers. Doing something like "variable = 7 AND 9" returns 79
>rather than 1.
you could paste two numbers together like this:
echo "3" | awk '{a=7; b=9; c=a "" b; print c}'
79
Chuck Demas
--
Eat Healthy | _ _ | Nothing would be done at all,
Stay Fit | @ @ | If a man waited to do it so well,
Die Anyway | v | That no one could find fault with it.
demas@theworld.com | \___/ | http://world.std.com/~cpd
|
|
0
|
|
|
|
Reply
|
demas
|
12/4/2003 11:47:19 AM
|
|
In article <n6vfowizl2.fsf@tt.oma.it.jyu.fi>,
Tapani Tarvainen <gn20031204T131637@tt.oma.it.jyu.fi> wrote:
>richdyer_2000@hotmail.com (Richard Dyer) writes:
>
>> I'm completely new to awk, so don't even know if this is possible. I
>> need to modify some old code so that it performs a bitwise "and" on
>> two numbers. Doing something like "variable = 7 AND 9" returns 79
>> rather than 1.
>
>There's no built-in binary AND in standard awk, you'll have to do it
>the hard way with a division loop, or call an external program.
>
>Here's an example of a division loop ANDer (not optimized in
>any way and slow as molasses, but it should work):
It looks like the OP used the phrase 'bitwise "and"' to mean that which most
of us would refer to as "string concatenation". This operation is, BTW,
included in all versions of AWK.
Anyway, all of the usable AWK implementations (GAWk & TAWK) do have built-in
bitwise operators.
|
|
0
|
|
|
|
Reply
|
gazelle
|
12/4/2003 1:23:49 PM
|
|
In article <bqn6s7$8jc$1@pcls3.std.com>,
Charles Demas <demas@TheWorld.com> wrote:
% you could paste two numbers together like this:
%
% echo "3" | awk '{a=7; b=9; c=a "" b; print c}'
In fact, the "" isn't needed, either. Just
c = a b
is sufficient. I wanted to point out that you need to be careful
if you want to use this in an expression without first assigning
it to another variable. Use parentheses!
a b * 7 # is 781 when a is 7 and b is 9
(a b) * 7 # is 711
--
Patrick TJ McPhee
East York Canada
ptjm@interlog.com
|
|
0
|
|
|
|
Reply
|
ptjm
|
12/4/2003 10:25:44 PM
|
|
Patrick TJ McPhee wrote:
> In article <bqn6s7$8jc$1@pcls3.std.com>,
> Charles Demas <demas@TheWorld.com> wrote:
>
> % you could paste two numbers together like this:
> %
> % echo "3" | awk '{a=7; b=9; c=a "" b; print c}'
>
> In fact, the "" isn't needed, either. Just
>
> c = a b
>
> is sufficient. I wanted to point out that you need to be careful
> if you want to use this in an expression without first assigning
> it to another variable. Use parentheses!
> a b * 7 # is 781 when a is 7 and b is 9
> (a b) * 7 # is 711
I hope you don't get that result.
a b * 7 -> 7 (9 * 7) -> 7 63 -> 763
and
(a b) * 7 -> (7 9) * 7 -> 79 * 7 -> 553
--
Regards,
---Robert
|
|
0
|
|
|
|
Reply
|
Robert
|
12/4/2003 11:50:33 PM
|
|
In article <12652ad6.0312040245.5b2221ab@posting.google.com>,
Richard Dyer <richdyer_2000@hotmail.com> wrote:
>I'm completely new to awk, so don't even know if this is possible. I
>need to modify some old code so that it performs a bitwise "and" on
>two numbers. Doing something like "variable = 7 AND 9" returns 79
>rather than 1.
If you're using gawk:
variable = and(7,9)
Otherwise, you'll need to use a division loop to convert the values to binary.
John
--
John DuBois spcecdt@armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/
|
|
0
|
|
|
|
Reply
|
spcecdt
|
12/5/2003 12:17:12 AM
|
|
In article <3fcfce88$0$1098$8eec23a@newsreader.tycho.net>,
John DuBois <spcecdt@deeptht.armory.com> wrote:
>In article <12652ad6.0312040245.5b2221ab@posting.google.com>,
>Richard Dyer <richdyer_2000@hotmail.com> wrote:
>>I'm completely new to awk, so don't even know if this is possible. I
>>need to modify some old code so that it performs a bitwise "and" on
>>two numbers. Doing something like "variable = 7 AND 9" returns 79
>>rather than 1.
>
>If you're using gawk:
>
>variable = and(7,9)
>
>Otherwise, you'll need to use a division loop to convert the values to binary.
See my other post in this thread. Apparently, the OP is asking about string
concatenation, not bitwise operations.
|
|
0
|
|
|
|
Reply
|
gazelle
|
12/5/2003 1:19:04 AM
|
|
In article <bqomq9$1h2$1@yin.interaccess.com>,
Kenny McCormack <gazelle@interaccess.com> wrote:
>In article <3fcfce88$0$1098$8eec23a@newsreader.tycho.net>,
>John DuBois <spcecdt@deeptht.armory.com> wrote:
>>In article <12652ad6.0312040245.5b2221ab@posting.google.com>,
>>Richard Dyer <richdyer_2000@hotmail.com> wrote:
>>>I'm completely new to awk, so don't even know if this is possible. I
>>>need to modify some old code so that it performs a bitwise "and" on
>>>two numbers. Doing something like "variable = 7 AND 9" returns 79
>>>rather than 1.
>>
>>If you're using gawk:
>>
>>variable = and(7,9)
>>
>>Otherwise, you'll need to use a division loop to convert the values to binary.
>
>See my other post in this thread. Apparently, the OP is asking about string
>concatenation, not bitwise operations.
Surprising how many people jumped at the keyword and didn't read
further to see if it applied.
Even more surprising was that two of them posted public replies.
Chuck Demas
--
Eat Healthy | _ _ | Nothing would be done at all,
Stay Fit | @ @ | If a man waited to do it so well,
Die Anyway | v | That no one could find fault with it.
demas@theworld.com | \___/ | http://world.std.com/~cpd
|
|
0
|
|
|
|
Reply
|
demas
|
12/5/2003 1:42:44 AM
|
|
gazelle@yin.interaccess.com (Kenny McCormack) writes:
> >richdyer_2000@hotmail.com (Richard Dyer) writes:
> >> I need to modify some old code so that it performs a bitwise "and" on
> >> two numbers. Doing something like "variable = 7 AND 9" returns 79
> >> rather than 1.
> It looks like the OP used the phrase 'bitwise "and"' to mean
> that which most of us would refer to as "string concatenation".
Perhaps. As I read it, however, he is complaining that "something
like 7 AND 9" returns 79 (concatenation), whereas he would want 1
(bitwise AND).
Let's hope he will clarify, guessing won't do much good.
--
Tapani Tarvainen
|
|
0
|
|
|
|
Reply
|
Tapani
|
12/5/2003 6:17:11 AM
|
|
Thanks to all of you for the help there.
I actually cheated in the end. I was using awk to generate a STOL
script from a text file. It eventually dawned on me that i could
therefore just get awk to print a bitwise and statement to the STOL
script and let STOL do all the maths.
Rich
|
|
0
|
|
|
|
Reply
|
richdyer_2000
|
12/5/2003 9:44:55 AM
|
|
In article <dLPzb.10187$cd.9894@news.cpqcorp.net>,
Robert Katz <katz@hp.com> wrote:
% Patrick TJ McPhee wrote:
% > a b * 7 # is 781 when a is 7 and b is 9
% > (a b) * 7 # is 711
%
% I hope you don't get that result.
It is if you use a b * 9, which is what I was thinking. Just ignore what
I write and go with what I'm thinking.
--
Patrick TJ McPhee
East York Canada
ptjm@interlog.com
|
|
0
|
|
|
|
Reply
|
ptjm
|
12/5/2003 8:22:27 PM
|
|
In article <n6r7zj94bc.fsf@tt.oma.it.jyu.fi>,
Tapani Tarvainen <gn20031205T081246@tt.oma.it.jyu.fi> wrote:
% Perhaps. As I read it, however, he is complaining that "something
% like 7 AND 9" returns 79 (concatenation), whereas he would want 1
% (bitwise AND).
I think you're probably right. The rest of us should pay closer attention.
% Let's hope he will clarify, guessing won't do much good.
I can't agree with this. Guessing is the best thing, and much more
interesting than knowing.
--
Patrick TJ McPhee
East York Canada
ptjm@interlog.com
|
|
0
|
|
|
|
Reply
|
ptjm
|
12/5/2003 8:25:20 PM
|
|
On 4 Dec 2003 02:45:30 -0800 in comp.lang.awk,
richdyer_2000@hotmail.com (Richard Dyer) wrote:
>I'm completely new to awk, so don't even know if this is possible. I
>need to modify some old code so that it performs a bitwise "and" on
>two numbers. Doing something like "variable = 7 AND 9" returns 79
>rather than 1.
AND is not an awk keyword, so awk assumes it is a variable and creates
it for you with the null string value "", which is then used in the
implicit concatenation "7" AND "9" resulting in "79".
You need to use awk's bitwise functions and(,), or(,), xor(,),
compl(), lshift(,), rshift(,). The statement "variable = and(7,9)"
will give you the result you are looking for.
--
Thanks. Take care, Brian Inglis Calgary, Alberta, Canada
Brian.Inglis@CSi.com (Brian dot Inglis at SystematicSw dot ab dot ca)
fake address use address above to reply
|
|
0
|
|
|
|
Reply
|
Brian
|
12/6/2003 6:30:08 AM
|
|
In article <bqomq9$1h2$1@yin.interaccess.com>,
Kenny McCormack <gazelle@interaccess.com> wrote:
>In article <3fcfce88$0$1098$8eec23a@newsreader.tycho.net>,
>John DuBois <spcecdt@deeptht.armory.com> wrote:
>>In article <12652ad6.0312040245.5b2221ab@posting.google.com>,
>>Richard Dyer <richdyer_2000@hotmail.com> wrote:
>>>I'm completely new to awk, so don't even know if this is possible. I
>>>need to modify some old code so that it performs a bitwise "and" on
>>>two numbers. Doing something like "variable = 7 AND 9" returns 79
>>>rather than 1.
>>
>>If you're using gawk:
>>
>>variable = and(7,9)
>>
>>Otherwise, you'll need to use a division loop to convert the values to binary.
>
>See my other post in this thread. Apparently, the OP is asking about string
>concatenation, not bitwise operations.
No, he was asking about bitwise operations. He said he wanted to do a 'bitwise
and', he showed the result he gets when he tried to do this using a nonexistant
binary AND operator (string concatenation, which he didn't want), and he showed
what he did want to get in the example he gave (the result of a bitwise and).
John
--
John DuBois spcecdt@armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/
|
|
0
|
|
|
|
Reply
|
spcecdt
|
12/7/2003 2:21:48 AM
|
|
John,
It's nice to see at least one person understands english as well as awk!
Rich
|
|
0
|
|
|
|
Reply
|
richdyer_2000
|
12/8/2003 1:25:12 PM
|
|
|
15 Replies
918 Views
(page loaded in 0.159 seconds)
Similiar Articles: Bitwise comparison of 2 numbers - comp.lang.awkI'm completely new to awk, so don't even know if this is possible. I need to modify some old code so that it performs a bitwise "and" on two numbers. ... Compare Two Number Arrays - comp.soft-sys.matlabBitwise comparison of 2 numbers - comp.lang.awk Compare Two Number Arrays - comp.soft-sys.matlab Bitwise comparison of 2 numbers - comp.lang.awk Compare Two Number Arrays ... a series of ANCOVA vs. MANOVA - comp.soft-sys.sasBitwise comparison of 2 numbers - comp.lang.awk... sas Bitwise comparison of 2 ... plotting two data series ... ANOVA is sufficient for comparison of groups? - comp.soft ... Assigning random numbers - comp.soft-sys.sasBitwise comparison of 2 numbers - comp.lang.awk Assigning random numbers - comp.soft-sys.sas Bitwise comparison of 2 numbers - comp.lang.awk Assigning random numbers ... Numbers do not print - comp.databases.filemakerBitwise comparison of 2 numbers - comp.lang.awk I need to modify some old code so that it performs a bitwise "and" on two numbers. ... ... paste two numbers together like ... random numbers between an upper and a lower limit - comp.soft-sys ...Assigning random numbers - comp.soft-sys.sas random numbers between an upper and a lower limit - comp.soft-sys ... Bitwise comparison of 2 numbers - comp.lang.awk ... plotting two data series of different number of elements - comp ...Bitwise comparison of 2 numbers - comp.lang.awk... numbers - comp.soft-sys.sas Bitwise comparison of 2 ... plotting two data series of different number of ... ... is, BTW ... Difference between SAR and IDIV??? - comp.lang.asm.x86Bitwise comparison of 2 numbers - comp.lang.awk Difference between SAR and IDIV??? - comp.lang.asm.x86 Bitwise comparison of 2 numbers - comp.lang.awk Difference between ... Bitwise operators - comp.lang.fortranBitwise comparison of 2 numbers - comp.lang.awk This operation is, BTW, included in all versions of AWK. Anyway, all of the usable AWK implementations (GAWk & TAWK) do ... Creating matrix of 1's and 0's based on a comparison of values in ...Bitwise comparison of 2 numbers - comp.lang.awk... the values to binary. > >See my other post in ... Creating matrix of 1's and 0's based on a comparison ... ... of two ... Difference beetwen awk and gawk? - comp.lang.awkBitwise comparison of 2 numbers - comp.lang.awk Difference beetwen awk and gawk? - comp.lang.awk Bitwise comparison of 2 numbers - comp.lang.awk Anyway, all of the usable ... AWK to C converter - comp.lang.awkBitwise comparison of 2 numbers - comp.lang.awk you could paste two numbers together like this: echo "3" | awk '{a=7; b=9; c=a ... Bitwise comparison of 2 numbers - comp ... Compare Two Images - comp.soft-sys.matlab... Compare Two Images - comp.soft-sys.matlab Compare Two Number Arrays - comp.soft-sys.matlab... Compare Two Number Arrays - comp.soft-sys.matlab Bitwise comparison of 2 ... concatenation with a for loop - comp.lang.verilogBitwise comparison of 2 numbers - comp.lang.awk... AND in standard awk, you'll have to do it the hard way with a division loop, or ... bitwise "and"' to mean that which ... compare alphanumeric number with integer problem - comp.lang ...Compare Two Number Arrays - comp.soft-sys.matlab In my actual problem both the arrays ... Packed Compare ... pcmpgtb works on signed integer. i.e. when performing comparison ... Bitwise comparison of 2 numbers - comp.lang.awk | Computer GroupI'm completely new to awk, so don't even know if this is possible. I need to modify some old code so that it performs a bitwise "and" on two numbers. ... Bitwise operation - Wikipedia, the free encyclopediaFor unsigned integers, the bitwise complement of a number is the "mirror reflection" of the ... In this we perform the comparison of two bits, being 1 if the two bits are ... 7/22/2012 7:20:41 AM
|