Offline
Sr. Member
Karma: 1
Posts: 462
I am a amateur.


« on: June 10, 2012, 08:15:12 pm » 
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
God Member
Karma: 17
Posts: 721


« Reply #1 on: June 10, 2012, 10:24:49 pm » 
Bitshift left and right are multiplication and division by powers of 2.



Logged





Pittsburgh, PA, USA
Offline
Faraday Member
Karma: 95
Posts: 4767
I learn a bit every time I visit the forum.


« Reply #2 on: June 11, 2012, 12:09:23 am » 
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
Edison Member
Karma: 19
Posts: 1041
Arduino rocks


« Reply #3 on: June 11, 2012, 12:13:09 am » 
In bash, the backtick (`) means "execute this and replace with stdout" but it's depreciated (the $(command) is preferred).



Logged





Global Moderator
Netherlands
Offline
Shannon Member
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...


« Reply #4 on: June 11, 2012, 01:13:45 am » 
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 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





Offline
Sr. Member
Karma: 1
Posts: 462
I am a amateur.


« Reply #5 on: June 11, 2012, 08:56:01 am » 
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 * 2 ^{3} = 272



Logged





Global Moderator
UK
Offline
Brattain Member
Karma: 290
Posts: 25795
I don't think you connected the grounds, Dave.


« Reply #6 on: June 11, 2012, 09:09:09 am » 
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
Sr. Member
Karma: 1
Posts: 462
I am a amateur.


« Reply #7 on: June 11, 2012, 09:33:19 am » 
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
Brattain Member
Karma: 290
Posts: 25795
I don't think you connected the grounds, Dave.


« Reply #8 on: June 11, 2012, 09:34:25 am » 
I don't know what kind of number b (11) for a 2 bitter There's a clue



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
Sr. Member
Karma: 1
Posts: 462
I am a amateur.


« Reply #9 on: June 11, 2012, 09:50:38 am » 
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
Brattain Member
Karma: 290
Posts: 25795
I don't think you connected the grounds, Dave.


« Reply #10 on: June 11, 2012, 09:57:43 am » 
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.


« 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
Shannon Member
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...


« Reply #11 on: June 11, 2012, 12:16:02 pm » 
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 (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 [ANDORXORNAND....] 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: 1a*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





Offline
Sr. Member
Karma: 1
Posts: 462
I am a amateur.


« Reply #12 on: June 11, 2012, 12:37:03 pm » 
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
Brattain Member
Karma: 290
Posts: 25795
I don't think you connected the grounds, Dave.


« Reply #13 on: June 11, 2012, 12:48:14 pm » 
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
Shannon Member
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...


« Reply #14 on: June 11, 2012, 12:53:17 pm » 
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





