How do I use the bitwise logical-or operator, |, in tcsh?
@ y = 1001; @ z = 0110; @ x = $y|$z 110: Command not found.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
Do you really want to do this in tcsh only? Csh and its derivative tcsh have many such quirks. You are better off with another shell like bash etc.
In this particular case, it appears that throwing in a pair of parenthesis keeps tcsh happy. This is also documented in the tcsh manual:
expr may contain the operators
+, etc., as in C. If expr contains
|then at least that part of expr must be placed within
mint13:~> echo $version tcsh 6.17.06 (Astron) 2011-04-15 (x86_64-unknown-linux) options wide,nls,dl,al,kan,rh,nd,color,filec mint13:~> @ y = 1001; @ z = 0110; mint13:~> @ x = $y|$z 110: Command not found. mint13:~> @ x = ($y|$z) mint13:~> echo $x 1007
You might already be aware of this — tcsh is reading 1001 and 0110 as decimal numbers. I don't know how to make it understand binary numbers.
I don't know about how to declare binary numbers; but your syntax is wrong.
The '@' had its own expression parser and somehow it confused with the primary parser if the expression is not parenthesised. According the man-page it need spaces in several cases include the bitwise operators. I suggest use spaces everywhere since it had many bugs there.
This will work smoothly:
@ y = 1001; @ z = 0110; @ x = ( $y | $z ); echo $x
Notice that, like C, number that starts with 0, are octals, so $z value is 72 in decimal.
Octals and hexadecimals are used because they ca representing the binary numbers exactly. An octal digit is representing 3 binary digits.
binary <--> octal 000 0 001 1 010 2 011 3 100 4 101 5 110 6 111 7
If y and z values that you wrote are in binary, this is the correct form (by using octals)
@ y = 011; @z = 006; @ x = ( $y | $z ); echo $x 15
There is another solution, use whatever command-line calculator you want by using back-quoted expression. I suggest to try calc, bc or awk with this order.
Example with calc:
set x = `calc '0b1001 | 0b110'`; echo $x 15
15 (dec) = 017 (oct) = 0xF (hex) = 0b1111 (bin)