Pages: [1] 2   Go Down
Author Topic: Convert bitmath into regular math?  (Read 1216 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Sr. Member
****
Karma: 1
Posts: 462
I am a amateur.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
« Last Edit: June 11, 2012, 10:04:18 am by dkl65 » Logged


Offline Offline
God Member
*****
Karma: 19
Posts: 785
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4813
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 220
Posts: 13836
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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 smiley
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

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Sr. Member
****
Karma: 1
Posts: 462
I am a amateur.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 308
Posts: 26465
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is a (01) and b (11) binary intergers, or decimal intergers?
"01" is an octal integer
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Sr. Member
****
Karma: 1
Posts: 462
I am a amateur.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 308
Posts: 26465
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I don't know what kind of number b (11)
Quote
for a 2 bitter
There's a clue   smiley-wink
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Sr. Member
****
Karma: 1
Posts: 462
I am a amateur.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: June 11, 2012, 10:03:56 am by dkl65 » Logged


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 308
Posts: 26465
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Quote
So either robtillaart is wrong, or I am wrong.
Which also is a binary proposition.
I'll leave you to figure that one.

Quote
(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.
« Last Edit: June 11, 2012, 10:00:17 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 220
Posts: 13836
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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

Quote
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 smiley-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 :  (a*b)
1 bit OR : (a+b - (a*b))
1 bit XOR:  (a+b - a*b *2)
1 bit NAND: 1-a*b
1 bit NOR: a*b - a - b
1 bit NXOR: 2*a*b -a - b

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


« Last Edit: June 11, 2012, 12:26:36 pm by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Sr. Member
****
Karma: 1
Posts: 462
I am a amateur.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 308
Posts: 26465
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 220
Posts: 13836
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1] 2   Go Up
Jump to: