Is it possible to use logarithms to convert numbers to binary? - binary

I'm a CS freshman and I find the division way of finding a binary number to be a pain. Is it possible to use log to quickly find 24, for instance, in binary?

If you want to use logarithms, you can.
Define log2(b) as log(b) / log(2) or ln(b) / ln(2) (they are the same).
Repeat the following:
Define n as the integer part of log2(b). There is a 1 in the nth position in the binary representation of b.
Set b = b - 2n
Repeat first step until b = 0.
Worked example: Converting 2835 to binary
log2(2835) = 11.47.. => n = 11
The binary representation has a 1 in the 211 position.
2835 - (211 = 2048) = 787
log2(787) = 9.62... => n = 9
The binary representation has a 1 in the 29 position.
787 - (29 = 512) = 275
log2(275) = 8.10... => n = 8
The binary representation has a 1 in the 28 position.
275 - (28 = 256) = 19
log2(19) = 4.25... => n = 4
The binary representation has a 1 in the 24 position.
19 - (24 = 16) = 3
log2(3) = 1.58.. => n = 1
The binary representation has a 1 in the 21 position.
3 - (21 = 2) = 1
log2(1) = 0 => n = 0
The binary representation has a 1 in the 20 position.
We know the binary representation has 1s in the 211, 29, 28, 24, 21, and 20 positions:
2^ 11 10 9 8 7 6 5 4 3 2 1 0
binary 1 0 1 1 0 0 0 1 0 0 1 1
so the binary representation of 2835 is 101100010011.

From a CS perspective, binary is quite easy because you usually only need to go up to 255. Or 15 if using HEX notation. The more you use it, the easier it gets.
How I do it on the fly, is by remembering all the 2 powers up to 128 and including 1. (The presence of the 1 instead of 1.4xxx possibly means that you can't use logs).
128,64,32,16,8,4,2,1
Then I use the rule that if the number is bigger than each power in descending order, that is a '1' and subtract it, else it's a '0'.
So 163
163 >= 128 = '1' R 35
35 !>= 64 = '0'
35 >= 32 = '1' R 3
3 !>= 16 = '0'
3 !>= 8 = '0'
3 !>= 4 = '0'
3 >= 2 = '1' R 1
1 >= 1 = '1' R 0
163 = 10100011.
It may not be the most elegant method, but when you just need to convert something ad-hoc thinking of it as comparison and subtraction may be easier than division.

Yes, you have to loop through 0 -> power which is bigger than you need and then take the remainder and do the same, which is a pain too.
I would suggest you trying recursion approach of division called 'Divide and Conquer'.
http://web.stanford.edu/class/archive/cs/cs161/cs161.1138/lectures/05/Small05.pdf
But again, since you need a binary representation, I guess unless you use ready utils, division approach is the simplest one IMHO.

Related

Big numbers Napier's location arithmetic

I have to represent this equation 2049*(M+N*100)+(M*N)*2800 using John Napier's Location Arithmetic and then calculate it using the notation's characteristic.
So for example I know that 29*11 would be (2*1)(2*1)(9*1)(9*1) = 3 3 10 10 = 4 11 = 24+ 211 = 16 + 2048 = 2064.
With that knowledge I can change most part of equation, but how can I calculate it with a big number like 2800?
Let's say M = 7 and N = 10.

Converting decimal floating number to binary

Can anyone please help me convert number (for example) 143,625 to binary? I've been searching through net for quite a long time but didn't find anything with good explanation.
Thanks in advance!
The integer part can be done by dividing by 2 repeatedly and keeping track of the remainder:
143 / 2 = 71 remainder 1
71 / 2 = 35 remainder 1
35 / 2 = 17 remainder 1
17 / 2 = 8 remainder 1
8 / 2 = 4 remainder 0
4 / 2 = 2 remainder 0
2 / 2 = 1 remainder 0
1 / 2 = 0 remainder 1
So the integer part is 10001111
For the fractional part, multiply it by 2 repeatedly and look at the integer part of the result:
.625 x 2 = 1.25 - we need the integer part, which is 1 - so far we have 0.1, we then disregard the 1 and look at 0.25
0.25 x 2 = 0.5 - so far we have 0.10, we look at 0.5
0.5 x 2 = 1.0 - we have 0.101, no decimal part so we're good.
The whole number is the integer part + the decimal part, so 10001111.101

Add 25 & 30 as binary number

Using 8 bit registers and signed magnitude representation.
I thought 25 in BCD is 010 0101 but my text book says it as 001 1001. Can somebody explain?
25 / 2 = 12r1 (12 with a remainder of 1)
12 / 2 = 6r0 (6 with a remainder of 0)
6 / 2 = 3r0 (3 with a remainder of 0)
3 / 2 = 1r1 (1 with a remainder of 0)
1 / 2 = 0r1 (0 with a remainder of 0)
So 11001 (working backward up the tree) is the binary equivalent to 25.
Another way to think about it is with powers of 2:
(1*16) + (1*8) + (0*4) + (0*2) + (1*1) = 25
And it's worth noting, just as in base 10, leading zeros do not change the value of a number. (00025 == 25) (0011001 == 11001).
The leading zeros are there in your case because your needing to populate an 8 bit register (there needs to be 8 binary digits regardless of their value).

octave: using find() on cell array {} subscript and assigning it to another cell array

This is an example in Section 6.3.1 Comma Separated Lists Generated from Cell Arrays of the Octave documentation (I browsed it through the doc command on the Octave prompt) which I don't quite understand.
in{1} = [10, 20, 30, 40, 50, 60, 70, 80, 90];
in{2} = inf;
in{3} = "last";
in{4} = "first";
out = cell(4, 1);
[out{1:3}] = find(in{1 : 3}); % line which I do not understand
So at the end of this section, we have in looking like:
in =
{
[1,1] =
10 20 30 40 50 60 70 80 90
[1,2] = Inf
[1,3] = last
[1,4] = first
}
and out looking like:
out =
{
[1,1] =
1 1 1 1 1 1 1 1 1
[2,1] =
1 2 3 4 5 6 7 8 9
[3,1] =
10 20 30 40 50 60 70 80 90
[4,1] = [](0x0)
}
Here, find is called with 3 output parameters (forgive me if I'm wrong on calling them output parameters, I am pretty new to Octave) from [out{1:3}], which represents the first 3 empty cells of the cell array out.
When I run find(in{1 : 3}) with 3 output parameters, as in:
[i,j,k] = find(in{1 : 3})
I get:
i = 1 1 1 1 1 1 1 1 1
j = 1 2 3 4 5 6 7 8 9
k = 10 20 30 40 50 60 70 80 90
which kind of explains why out looks like it does, but when I execute in{1:3}, I get:
ans = 10 20 30 40 50 60 70 80 90
ans = Inf
ans = last
which are the 1st to 3rd elements of the in cell array.
My question is: Why does find(in{1 : 3}) drop off the 2nd and 3rd entries in the comma separated list for in{1 : 3}?
Thank you.
The documentation for find should help you answer your question:
When called with 3 output arguments, find returns the row and column indices of non-zero elements (that's your i and j) and a vector containing the non-zero values (that's your k). That explains the 3 output arguments, but not why it only considers in{1}. To answer that you need to look at what happens when you pass 3 input arguments to find as in find (x, n, direction):
If three inputs are given, direction should be one of "first" or
"last", requesting only the first or last n indices, respectively.
However, the indices are always returned in ascending order.
so in{1} is your x (your data if you want), in{2} is how many indices find should consider (all of them in your case since in{2} = Inf) and {in3}is whether find should find the first or last indices of the vector in{1} (last in your case).

What are w-bit words?

What are w-bit words in computer architecture ?
For two 7 bit words
1011001 = A
1101011 = B , how does multiplication returns
10010100110011 ?
Isn't there simple binary multiplication involved in these ?
Please provide an example.
w-bit is just the typical nomenclature for n-bit because w is usually short for word size
Both adding and multiplying are done just the same as in decimal (base 10). You just need to remember this truth table:
Multiplying
-----------
0 x 0 = 0
0 x 1 = 0
1 x 0 = 0
1 x 1 = 1
Adding
-----------
0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 0 (w/ carry)
First adding. To add, you add just like you would in normal arithmetic, except follow the truth table above:
00000101 = 5
+ 00000011 = 3
--------------
00001000 = 8
How this works is that you start from the right and work left. 1 + 1 = 0, but you carry a 1 over to the next column. So the next column is 0 + 1, which would be 1, but since you carried another 1 from the previous column, its really 1 + 1, which is 0. You carry a 1 over the next column, which is 1 + 0, but really 1 + 1 because of the carry. So 0 again and finally move the 1 to the next column, which is 0 + 0, but because of our carry, becomes 1 + 0, which is 1. So our answer is 1000, which is 8 in decimal. 5 + 3 = 8, so we know we are right.
Next, multiplying:
00000101 = 5
x 00000011 = 3
----------
101 = 5
+ 1010 = 10
----------
1111 = 15
How this works is you multiply the top number 00000101 by the right most digit in the second row. So 00000011 is our second row and 1 is the right most digit, so 00000101 times 1 = 101. Next you put a 0 placeholder in the right most column below it, just like in normal multiplication. Then you multiply our top original number 00000101 by the next digit going left in our original problem 00000011. Again it produce 101. Next you simply add 101 + 1010 = 1111 ...That is the answer
Yes, it's simple binary multiplication:
>>> 0b1011001
89
>>> chr(_)
'Y'
>>> 0b1101011
107
>>> chr(_)
'k'
>>> ord('Y') * ord('k')
9523
>>> bin(_)
'0b10010100110011'
If you want to multiply, you simply do the multiplication the same as with decimal numbers, except that you have to add the carries in binary:
1011001
x1101011
-------
1011001
1011001.
0000000..
1011001...
0000000....
1011001.....
1011001......
--------------
10010100110011
w-bit words aren't anything by themselves. Assuming that the value of w has been previously defined in the context in which "w-bit word" is used, then it simply means a word that is composed of w bits. For instance:
A version of RC6 is more accurately specified as RC6-w/r/b where the word size
is "w" bits, encryption consists of a nonnegative number of rounds "r," and
"b" denotes the length of the encryption key in bytes. Since the AES
submission is targetted at w=32, and r=20, we shall use RC6 as shorthand to
refers to such versions.
So in the context of that document, a "w-bit word" is just a 32-bit value.
As for your multiplication, I'm not sure what you are asking. Google confirms the result as correct:
1011001 * 1101011 = 10010100110011