When I try to convert the decimal value 1012 to hexadecimal, I get the value 3F4. However, in the class the teacher got the value 100C.
Could anyone tell me how that is possible?
Well 12 in hexadecimal is C...
So 1 = 1, 0 = 0, 12 = C
1 0 12 = 1 0 0xC
That's the only way I can see it being possible.
Well 100C if I am not wrong, is not 1012. 100C is:
1 * (16^3) + 12 * (16^0) = (4096 + 12) = 4108
Are you sure that your teacher gave that result?
Related
There is a lot of ways to find a digital root of a number and they are all similar to each other, but I can't understand the following one:
int digitalRoot(int n)
if (n < 10)
return n;
else
return digitalRoot(n / 10 + n % 10);
I see what the algorithm does, but can't understand why it works, how digital root of a number is related to sum of n / 10 + n % 10. Maybe someone could explain it to me in simple terms if there is a simple explanation?
I'm hard to see any relations between the following ways of getting the digital root, but they give the same result and that's exactly what I'm trying to understand..
1729 => 1 + 7 + 2 + 9 = 19
19 => 1 + 9 = 10
10 => 1 + 0 = 1
and
1729 => 172 + 9 = 181
181 => 18 + 1 = 19
19 => 1 + 9 = 10
10 => 1 + 0 = 1
Maybe an example explains better:
Let n be 1234
First call to function returns 123 + 4
Now n=127, second call returns 12 + 3+4
Now n=19, third call returns 1 + 2+3+4
The result is 1 after fourth call.
So, it basically adds up all the digits.
My array is 1D m in length. say m = 16
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
The way I actually interpret the array is n x n = m
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
I require to read the array in this manner due to the way my physical environment is set up
0 4 8 12 13 9 5 1 2 6 10 14 15 11 7 3
What I came up with works but I really don't think it is the best way to do this:
bool isFlipped = true;
int x = 0; x < m; x++
if(isFlipped)
newLine[x] = line[((n-1)-x%n)*n + x/n)]
else
newLine[x] = line[x%n*n +x/n]
if(x != 0 && x % n == 0)
isFlipped = !isFlipped
This gives me the required result but I really think there is a way to get rid of this boolean by purely using a math formula. I am stuffing this into a 8kb microcontroller and I need to conserve as much space as I can because I will have some bluetooth communication and more math going into it later on.
Edit:
Thanks to a user I got to a one line solution-ish. (the below would replace the lines in the for-loop)
c=x/n
newLine[x] = line[((c+1)%2)*((x%n)*n+c) + (c%2)*((n-1)-2*(x%n))*n ];
You should be able to utilize the fact that odd columns in the n*n matrix are read from down up, and even columns are read from up down.
A number at index x in newLine is located in column number c=floor(x/n) in the n*n matrix. c%2 is 0 for even columns and 1 for odd columns. So something like this should work:
int c = x/n;
newLine[x] = line[(x%n)*n + (c%2)*((n-1)-2*(x%n))*n + c];
This question already has answers here:
Converting binary to hexadecimal?
(5 answers)
Closed 9 years ago.
I have seen many people get confused about how to convert base 2 to base 16 directly. In this tutorial I will explain how to convert a Binary number to a Hexadecimal number in 5 easy steps.
1) When you have a number in base 2, all digits must be either 0 or 1. If you have a digit(s) that isn't 0 or 1, your number is not in base 2 (Binary) and this tutorial won't be of use for you.
2) Make sure the length of you number is divisible by 4 (4,8,12,16 etc...). In this tutorial I will use 10001111011 in base 2 as the base number. Notice there there are only 11 digits. to make it divisible by 4 we will add a 0 to the left side of the number and check if the length is divisible by 4, keep on adding 0's until it is divisible.
3) Part your base 2 number into groups of four. In our case, 010001111011 will be 0100 0111 1011.
4) Now use the following table to convert each group of four digits to its matching value in base 16:
0000 = 0
0001 = 1
0010 = 2
0011 = 3
0100 = 4
0101 = 5
0110 = 6
0111 = 7
1000 = 8
1001 = 9
1010 = A
1011 = B
1100 = C
1101 = D
1110 = E
1111 = F
5) As a reminder, out number was 0100 0111 1011. Then 0100=4, 0111=7, 1011=B. Therefore. 010001111011 in base 2 is 47B in base 16(hexadecimal).
So I was practicing my binary subtraction. It's been a long while since my first exam and I decided to create my own tricky binary subtraction and I came up with this one:
1100
-1101
Of course the "borrowing trick" does not work for this problem at least I could not get it to work. Is my only choice to flip the bits of the second binary number(the bottom one) and then add a one basically doing 2's complement so 1101 becomes 0011. Then add the primary binary number(1100) with the 2's complement representation(0011) which means it would look like this:
1100 (-4) assume 2's complement
+ 0011 (3) assume 2's complement
sum:1111 (-1) assume 2's complement
I just need confirmation on this problem since its been a long time since I did binary subtraction.
1100
-1101
0 - 1 = 1 (borrow 1)
1100
-1101
1
=====
1
0 - 0 - 1 = 1 (borrow 1)
1100
-1101
11
=====
11
1 - 1 - 1 = 1 (borrow 1)
1100
-1101
111
=====
111
1 - 1 - 1 = 1 (borrow 1)
1100
-1101
1111
=====
1111
The result is 1111 with 1 borrowed. In terms of unsigned arithmetic, this means that either the result underflowed or you need to borrow from the next significant digit. (In terms of signed arithmetic there is no overflow as you have also borrowed the second bit and the calculation corresponds to -4 - -3 = -1.)
I'm trying to understand the reason for a rule when converting.
I'm sure there must be a simple explanation, but I can't seem to wrap my head around it.
Appreciate any help!
Converting from base10 to any other base is done like this:
number / desiredBase = number + remainder
You do this until number = 0.
But after all of the calculations, you have to take all the remainders upside down. I don't understand why.
For example: base10 number to base2
11 / 2 = 5 + 1
5 / 2 = 2 + 1
2 / 2 = 1 + 0
1 / 2 = 0 + 1
Why is the correct answer: 1011 and not 1101 ?
I know it's a little petty, but it would really help me remember better if I could understand this.
Think of the same in decimal system, even if it doesn't make that much sense to actually do the math in this case :)
1234 / 10 = 123 | 4
123 / 10 = 12 | 3
12 / 10 = 1 | 2
1 / 10 = 0 | 1
Every time you divide, you strip the least significant digit, so the first result, is the least significant result -- digit on the right.
Because 11 =
1 * 2 ^ 3 + 0 * 2 ^ 2 + 1 * 2 ^ 1 + 1 * 2 ^ 0 (1011)
and not
1 * 2 ^ 3 + 1 * 2 ^ 2 + 0 * 2 ^ 1 + 1 * 2 ^ 0 (1101)