Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bitwise operations in BC?

$ bc
BC> ibase=2
BC> 110&101                     // wanna get 100
(standar_in) 8: syntax error

Wikipedia informs that the ops are "|, & and ^". It may be that they work only in certain BC-types or I misread something.

like image 791
otto Avatar asked Jun 01 '10 23:06

otto


People also ask

Can you do bitwise operations in C?

In the C programming language, operations can be performed on a bit level using bitwise operators. Bitwise operations are contrasted by byte-level operations which characterize the bitwise operators' logical counterparts, the AND, OR, NOT operators.

How many bitwise operators are in C?

There are six different types of Bitwise Operators in C.

What is bitwise operator in C programming?

The ^ (bitwise XOR) in C or C++ takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different. The << (left shift) in C or C++ takes two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift.


1 Answers

Despite bc won't do it, you can use arithmetic expansion directly on the terminal if you use bash.

To XOR 44 and 61, you can do:

echo $((44^61))

If you want to use binary code, then:

echo $((2#110^2#101))

See Numerical Constants for changing the base.

See bitwise operators section to peep at available operators.

like image 73
user3000327 Avatar answered Sep 22 '22 16:09

user3000327