Convert bitmath into regular math?

Hello!
I was wondering if bitmath (bitshift left, bitshift right, OR, XOR, AND, NOT) and modulo can be expressed using regular math (add, subtract, multiply, divide). I know that NOT in regular math is (-x)-1. How about the other bitwise operations?

Is the symbol ` and $ ever used in Arduino? What is it used for?

Thanks!

Bitshift left and right are multiplication and division by powers of 2.

The bitmath is generally more efficient and never that I know of less so.

In bash, the backtick (`) means "execute this and replace with stdout" but it's depreciated (the $(command) is preferred).

I was wondering if bitmath (bitshift left, bitshift right, OR, XOR, AND, NOT) can be expressed using regular math

I think that in theory all can be expressed in regular math but the complexity involved would explode for some operations. (Never tried / investigated disclaimer :slight_smile:
I even think you need to hold state in between

the bitshifts are easy, as is the NOT, However to express an AND in normal math would include isolation of individual bits by using powers of 2 for every bit to isolate them and then multiply the bits on the same position and add the results.

for a 2 bitter the formula involved 4 divisions and 1 multiply - and that is the easiest but 1

a= 01
b= 11
c = (a/2 * b/2)*2 + (a%2 * b%2); // c = b & a

robtillaart:
a= 01
b= 11
c = (a/2 * b/2)*2 + (a%2 * b%2); // c = b & a

Is a (01) and b (11) binary intergers, or decimal intergers?

34 << 3 = 34 * 23 = 272

Is a (01) and b (11) binary intergers, or decimal intergers?

"01" is an octal integer

01 (octal), 0x1 (hexadecimal), 1 (decimal) and 0b1 (binary) is all the same. However, I don't know what kind of number b (11) is. If I guess wrong, the outcome will be very different.

I don't know what kind of number b (11)

for a 2 bitter

There's a clue :wink:

Oh! I thought that "bitter" was refering to the taste, or something harsh / bad. Instead, it means "2 bits". So the numbers must be binary! If they are binary...

(1/2 * 3/2)*2 + (1%2 * 3%2) = 2.5
3 & 1 = 1

So either robtillaart is wrong, or I am wrong. Also, % (modulo) is not regular math.

a= 01
b= 11
c = (a/2 * b/2)*2 + (a%2 * b%2);

c = (0 * 1)* 2 + (1 * 1) = (0 * 2) + 1 = 1

So either robtillaart is wrong, or I am wrong.

Which also is a binary proposition.
I'll leave you to figure that one.

(1/2 * 3/2)*2 + (1%2 * 3%2) = 2.5

It's a two bit machine - difficult to represent fractions and two bits of significance in two bits.

Is a (01) and b (11) binary intergers, or decimal intergers?

The number a and b are meant as binary numbers. I should have written:

a= b01
b= b11
c = (a/2 * b/2)*2 + (a%2 * b%2); // 8 operators

c = (0 * 1)* 2 + ( 1 * 1) = 0 + 1 = 1 == 01 ==> c = b & a = 01 & 11 = 01

Also, % (modulo) is not regular math.

Since when? What is regular math according to you?

intermezzo regular math?
A multiplication is a repeated addition, and an addition is a repeated increment by 1.
so in the end everything is repeated an increment by 1 (that sounds like a turing machine!!)

% can be expressed as a subtraction, division and a multiply. a % b = ( a - (a/b)*b)

a= b01
b= b11
c = (a/2 * b/2)*2 + (a- (a/2)*2) * (b- (b/2)*2); // 12 operators

Furthermore I assumed the integer division, which makes 1/2 => 0

So yes, I was wrong taking not enough time to explain this properly :wink:
(just like this post)

update
So if you want to rewrite bit operators to regular math you must first

  1. isolate the individual digits
  2. do the [AND-OR-XOR-NAND-....] operation
  3. merge the outcomes

1 bit AND : (ab)
1 bit OR : (a+b - (a
b))
1 bit XOR: (a+b - ab 2)
1 bit NAND: 1-a
b
1 bit NOR: a
b - a - b
1 bit NXOR: 2ab -a - b

Formula's for ANDing 3 bits numbers (higher) is left as an exercise for the reader

robtillaart:
What is regular math according to you?

Arithmetic that can be done with a standard calculator. Add, subtract, multiply, divide, square root are the only operations on a standard calculator. Powers are also accepted.

Children are sometimes given dictionaries that miss out all the "rude" words.
Just because your calculator doesn't include all arithmetic operation doesn't mean modulo operations are not members of the set of arithmetic operations.

as shown above you only need subtract add and multiply for the bit operation

for the isolation of a single bits you need (multiple) division(s) and subtractions.

for the merge the + and * are used.

The isolation of bit n can be done by the formula : a/ipow(2,n) - (a/ipow(2,n+1))*2

ipow is an integer power (can by optimized by using consts 1,2,4,8,16, etc)

34 << 3
vs
34 * 23

IF constants are used then the compiler turns either one into 272

IF the values are in variables that may change

A << B is very few cycles, the majority just to put A and B into registers and 1 cycle to shift

A * BC is many cycles

DKL65, you will be far better off just learning bit math than twisting your brain trying to make a few cheap calculator functions do the job.

The Windows calculator can be put into Scientific mode under View, you get many new buttons. There are other free calculators available. For Linux the term more and better comes to mind.

Back in the 80's I had at least 2 "programmer's" hand calculators and one that was a watch.

Print yourself a cheat sheet to refer to. After enough glances/look-ups (which okay may take more than a few weeks if you use it rarely) you won't need it any more.

You can do hex bits with 4 fingers, which are on the table are 1's and which are lifted are zeros. Count 0 to 15 on 4 fingers alone, 0 to 255 with two hands. Not bad when so many people need to take their shoes off to count past 10.

There is a "programmer calculator" on Windows 7 that calculates bitshifts, logic gates, and even convert different types of intergers. I use it all the time. I just asked the question out of curiousity.

AWOL:
Just because your calculator doesn't include all arithmetic operation doesn't mean modulo operations are not members of the set of arithmetic operations.

I didn't say modulo wasn't a legit arithmetic operation (it's actually a useful one); I just said that it wasn't on a standard calculator. When I want to calculate bitmath, I use my brain, or Window 7's Programmer Calculator. I wonder if there are real programmer calculators that people sell.

I wonder if there are real programmer calculators that people sell.

I've got a 27 year old Casino fx-570 you can buy.
Still works.
Like this

real programmers calculators

Most scientific calculators support binary/octal/hex math

currently - realcalc scientific calculator on my android [12 digits in hex mode] - payed version => add your own conversion functions and constants
supports and or xor not nand nor xnor +-*/ sqrt power

before a TANDY EC4014 - 10 digits display + solar was the trigger (still in use)
before a Casio fx-785P - programmable in some basic dialect and it has an IO port (still to be investigated :slight_smile:
before another Casio ???
before a slide ruler - Slide rule - Wikipedia - (still have one, not in use)
before a pen and paper (still in use)