Unix Shell - Are there any Unix utilities which have bitwise operators ?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > November 2006 > Are there any Unix utilities which have bitwise operators ?





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]

Author Are there any Unix utilities which have bitwise operators ?
Spiros Bousbouras

2006-11-28, 7:24 pm

Are there any Unix utilities which have bitwise operators ?
I'm referring to the operators & , | , ^ (exclusive or) of C. I
was under the impression that bc had them but apparently
not. Awk doesn't have them either. Anything else (including
shells) ?

To put it otherwise what would be the quickest way to get the
result of 12 & 45 (or 12 | 45 or 12 ^ 45) from the command line ?

Casper H.S. Dik

2006-11-28, 7:24 pm

"Spiros Bousbouras" <spibou@gmail.com> writes:

>Are there any Unix utilities which have bitwise operators ?
>I'm referring to the operators & , | , ^ (exclusive or) of C. I
>was under the impression that bc had them but apparently
>not. Awk doesn't have them either. Anything else (including
>shells) ?


>To put it otherwise what would be the quickest way to get the
>result of 12 & 45 (or 12 | 45 or 12 ^ 45) from the command line ?


perl?

perl -e 'print 12 | 45'


Casper
Stephane CHAZELAS

2006-11-28, 7:24 pm

2006-11-28, 12:57(-08), Spiros Bousbouras:
> Are there any Unix utilities which have bitwise operators ?
> I'm referring to the operators & , | , ^ (exclusive or) of C. I
> was under the impression that bc had them but apparently
> not. Awk doesn't have them either. Anything else (including
> shells) ?
>
> To put it otherwise what would be the quickest way to get the
> result of 12 & 45 (or 12 | 45 or 12 ^ 45) from the command line ?

[...]

Standard sh (as opposed to Bourne sh) should support it as part
of their arithmetic expansion.

echo "$((12&45))"

--
Stéphane
Janis Papanagnou

2006-11-28, 7:24 pm

Spiros Bousbouras wrote:
> Are there any Unix utilities which have bitwise operators ?
> I'm referring to the operators & , | , ^ (exclusive or) of C. I
> was under the impression that bc had them but apparently
> not. Awk doesn't have them either. Anything else (including
> shells) ?
>
> To put it otherwise what would be the quickest way to get the
> result of 12 & 45 (or 12 | 45 or 12 ^ 45) from the command line ?
>


ksh93...

$ print $((12&45)) $((12|45)) $((12^45))
12 45 33

$ print $((3&6)) $((3|6)) $((3^6))
2 7 5

(just tried)

Janis
Bill Marcum

2006-11-28, 7:24 pm

On 28 Nov 2006 12:57:55 -0800, Spiros Bousbouras
<spibou@gmail.com> wrote:
> Are there any Unix utilities which have bitwise operators ?
> I'm referring to the operators & , | , ^ (exclusive or) of C. I
> was under the impression that bc had them but apparently
> not. Awk doesn't have them either. Anything else (including
> shells) ?
>
> To put it otherwise what would be the quickest way to get the
> result of 12 & 45 (or 12 | 45 or 12 ^ 45) from the command line ?
>

gforth -e "12 45 and . cr bye"


--
Kirkland, Illinois, law forbids bees to fly over the village or through
any of its streets.
Spiros Bousbouras

2006-11-28, 7:24 pm

Stephane CHAZELAS wrote:
> Standard sh (as opposed to Bourne sh) should support it as part
> of their arithmetic expansion.


I'm not sure what counts as standard sh. I thought
that sh referred to the Bourne shell.

Spiros Bousbouras

2006-11-28, 7:24 pm

Janis Papanagnou wrote:
> Spiros Bousbouras wrote:
>
> ksh93...
>
> $ print $((12&45)) $((12|45)) $((12^45))
> 12 45 33
>
> $ print $((3&6)) $((3|6)) $((3^6))
> 2 7 5


Thanks , that works on Bash shell too.

Spiros Bousbouras

2006-11-28, 7:24 pm

Bill Marcum wrote:
> On 28 Nov 2006 12:57:55 -0800, Spiros Bousbouras
> <spibou@gmail.com> wrote:
> gforth -e "12 45 and . cr bye"


That's interesting to know but I wouldn't count Forth
as a Unix utlity ; it's something you'd have to download.

But for my general education what does the cr do ?

Kenan Kalajdzic

2006-11-28, 7:24 pm

Spiros Bousbouras <spibou@gmail.com> wrote:
> Are there any Unix utilities which have bitwise operators ?
> I'm referring to the operators & , | , ^ (exclusive or) of C. I
> was under the impression that bc had them but apparently
> not. Awk doesn't have them either. Anything else (including
> shells) ?
>
> To put it otherwise what would be the quickest way to get the
> result of 12 & 45 (or 12 | 45 or 12 ^ 45) from the command line ?


The solution presented below uses only the standard tools. It relies on
bc for the calculation(s), making it possible to use very large numbers.
It is far from "the quickest" way to get the job done, but it should be
*very* portable.

For the sake of clarity, we'll use variables to hold the operand values:

a=12
b=45

In order to perform the bitwise AND operation, we can enter the following
command line:

( echo 'ibase=2'; echo "obase=2; $a; $b" \
| bc | sed '/\\$/{N;s/[^0-9]//g;}' \
| sed 'N;s/[^0-9]/+/' | bc | tr '21' '10' ) \
| bc

The bitwise OR is almost equal, except for the arguments of tr:

( echo 'ibase=2'; echo "obase=2; $a; $b" \
| bc | sed '/\\$/{N;s/[^0-9]//g;}' \
| sed 'N;s/[^0-9]/+/' | bc | tr '2' '1' ) \
| bc

Finally, the bitwise XOR:

( echo 'ibase=2'; echo "obase=2; $a; $b" \
| bc | sed '/\\$/{N;s/[^0-9]//g;}' \
| sed 'N;s/[^0-9]/+/' | bc | tr '2' '0' ) \
| bc

--
Kenan Kalajdzic
Janis Papanagnou

2006-11-29, 1:30 am

Spiros Bousbouras wrote:
> Stephane CHAZELAS wrote:
>
>
> I'm not sure what counts as standard sh. I thought
> that sh referred to the Bourne shell.


Nowadays 'sh' seems mostly to be associated with POSIX shell.

Janis
Icarus Sparry

2006-11-29, 1:30 am

On Tue, 28 Nov 2006 16:46:37 -0800, Spiros Bousbouras wrote:

> Bill Marcum wrote:
>
> That's interesting to know but I wouldn't count Forth
> as a Unix utlity ; it's something you'd have to download.
>
> But for my general education what does the cr do ?


"cr" outputs a newline.
The "." prints the top of stack (and destroys it).
"bye" usually exits the forth system.
Stephane CHAZELAS

2006-11-29, 7:28 am

2006-11-28, 16:42(-08), Spiros Bousbouras:
> Stephane CHAZELAS wrote:
>
> I'm not sure what counts as standard sh. I thought
> that sh referred to the Bourne shell.


They was true 15 years ago. sh has been different things over
the Unix history: thomson, maschey, Bourne shell... whose
implementations varied from system to system.

There has been a (actually several) standardisation effort some
time ago. Now sh as well as the system interface and the
/standard/ utilities are specified by a (now) publicly available
document.

The /standard/ "sh" command specification is a superset of the
Bourne shell based on a subset of the Korn shell. bash, ksh,
newer ashes and to some extent zsh implement that specification
(when called as "sh" at least).

All modern Unices have such a sh. Some few Unices (such as
Solaris) chose to keep the Bourne shell as /bin/sh for backward
compatibility and install the real standard sh elsewhere (such
as /usr/xpg4/bin/sh).

It's true people often confuse sh with the Bourne shell, but
that's incorrect.

--
Stéphane
unixkernelpanic@gmail.com

2006-11-29, 7:28 am

Spiros Bousbouras wrote:
> Are there any Unix utilities which have bitwise operators ?
> I'm referring to the operators & , | , ^ (exclusive or) of C. I
> was under the impression that bc had them but apparently
> not. Awk doesn't have them either. Anything else (including
> shells) ?
>
> To put it otherwise what would be the quickest way to get the
> result of 12 & 45 (or 12 | 45 or 12 ^ 45) from the command line ?


ksh or bash:

$ echo $(( 7 & 4 ))
4
$ echo $(( 7 ^ 4 ))
3
$ echo $(( 7 | 4 ))
7
$

Perl:

$ PERL -e 'printf "%d\n", (7 & 4);'
4
$ PERL -e 'printf "%d\n", (7 ^ 4);'
3
$ PERL -e 'printf "%d\n", (7 | 4);'
7
$

csh:

% @ x = ( 7 & 4) ; echo $x
4
% @ x = ( 7 ^ 4) ; echo $x
3
% @ x = ( 7 | 4) ; echo $x
7
%

Spiros Bousbouras

2006-11-29, 7:22 pm

Stephane CHAZELAS wrote:
> 2006-11-28, 16:42(-08), Spiros Bousbouras:
>
> They was true 15 years ago. sh has been different things over
> the Unix history: thomson, maschey, Bourne shell... whose
> implementations varied from system to system.
>
> There has been a (actually several) standardisation effort some
> time ago. Now sh as well as the system interface and the
> /standard/ utilities are specified by a (now) publicly available
> document.
>
> The /standard/ "sh" command specification is a superset of the
> Bourne shell based on a subset of the Korn shell. bash, ksh,
> newer ashes and to some extent zsh implement that specification
> (when called as "sh" at least).
>
> All modern Unices have such a sh. Some few Unices (such as
> Solaris) chose to keep the Bourne shell as /bin/sh for backward
> compatibility and install the real standard sh elsewhere (such
> as /usr/xpg4/bin/sh).
>
> It's true people often confuse sh with the Bourne shell, but
> that's incorrect.


That was very enlightening , thank you. What's the name
of the document and where do I find it ?

Daniel Rock

2006-11-29, 7:22 pm

Spiros Bousbouras <spibou@gmail.com> wrote:
> That was very enlightening , thank you. What's the name
> of the document and where do I find it ?


http://www.opengroup.org/onlinepubs/009695399/toc.htm

--
Daniel
Chris F.A. Johnson

2006-11-29, 7:22 pm

On 2006-11-29, Spiros Bousbouras wrote:
> Stephane CHAZELAS wrote:

....
....[vbcol=seagreen]
> That was very enlightening , thank you. What's the name
> of the document and where do I find it ?


<http://www.opengroup.org/onlinepubs...s/contents.html>

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Spiros Bousbouras

2006-11-30, 7:23 pm

Kenan Kalajdzic wrote:
> Spiros Bousbouras <spibou@gmail.com> wrote:
>
> The solution presented below uses only the standard tools. It relies on
> bc for the calculation(s), making it possible to use very large numbers.
> It is far from "the quickest" way to get the job done, but it should be
> *very* portable.
>
> For the sake of clarity, we'll use variables to hold the operand values:
>
> a=12
> b=45
>
> In order to perform the bitwise AND operation, we can enter the following
> command line:
>
> ( echo 'ibase=2'; echo "obase=2; $a; $b" \
> | bc | sed '/\\$/{N;s/[^0-9]//g;}' \
> | sed 'N;s/[^0-9]/+/' | bc | tr '21' '10' ) \
> | bc


That's quite clever. But what does the sed '/\\$/{N;s/[^0-9]//g;}'
part do ? It seems to me that it will work just fine without it.

Spiros Bousbouras

2006-11-30, 7:23 pm

Kenan Kalajdzic wrote:
> Spiros Bousbouras <spibou@gmail.com> wrote:
>
> The solution presented below uses only the standard tools. It relies on
> bc for the calculation(s), making it possible to use very large numbers.
> It is far from "the quickest" way to get the job done, but it should be
> *very* portable.
>
> For the sake of clarity, we'll use variables to hold the operand values:
>
> a=12
> b=45
>
> In order to perform the bitwise AND operation, we can enter the following
> command line:
>
> ( echo 'ibase=2'; echo "obase=2; $a; $b" \
> | bc | sed '/\\$/{N;s/[^0-9]//g;}' \
> | sed 'N;s/[^0-9]/+/' | bc | tr '21' '10' ) \
> | bc


That's quite clever. But what does the sed '/\\$/{N;s/[^0-9]//g;}'
part do ? It seems to me that it will work just fine without it.

Spiros Bousbouras

2006-11-30, 7:23 pm

Spiros Bousbouras wrote:
> Kenan Kalajdzic wrote:
>
> That's quite clever. But what does the sed '/\\$/{N;s/[^0-9]//g;}'
> part do ? It seems to me that it will work just fine without it.


I get it now ; it's for when bc outputs a number over
several lines. Unfortunately it doesn't work if the
output of a single number occupies more than 2 lines.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com