Byte Math Calculation - math

Let's say we have a setting byte that equals and you know that b1-b4 are always 1 or 0
b1 + b2*2 + b3*8 + b4*16
You receive a value doesn't matter what value, let's just say 25,
How would you figure out what bytes are set?

A bit can be either 0 or 1. Start from the biggest number (16) and subtract from the target decimal number if target is bigger than or equal to it. Otherwise do nothing and set that bit to 0.
For example, for 25:
is 16 <= 25? : Yes, therefore b4 = 1 Subtract 16 from 25.
is 8 <= (25-16) : Yes, therefore b3 = 1 Subtract 8 from 9.
is 2 <= (25-16-8) : No, therefore b2 = 0
is 1 <= (25-16-8) : Yes, therefore b1 = 1 Subtract 1 from 1.
So it is: 1101 (b4 b3 b2 b1)
It is exactly the same logic applied when making transformation from decimal numbers to binary numbers. However, b3 is multiplied with 8, not 4 in your question (why though?)
What is the logic behind this?
Well, when there is N in your number (like 16 in 25), we know that all other numbers below N, ( N/2 N/4 N/8 etc.) cannot be summed up to N. Their sum will be N - 1. You can find it with an easy calculation: calculation

Related

No. of perfect squares between 1 and A such that A>=1 is equal to integral part of sqrt[A]. Could someone help me understand why this works?

No. of perfect squares between 1 and A such that A>=1 is equal to integral part of sqrt[A].
I tried few examples to convince myself but still could someone help me understand why this works?
Call a the integral part of sqrt(A).
If b is an integer such that 1 <= b <= a, then b² is a perfect square between 1 and A.
Conversely, if B is a perfect square between 1 and A, then there exists a unique nonnegative integer b such that b² = B, and from 1 <= B <= A it follows that 1 <= b <= a.
Thus there is a bijection between the following two sets:
The set of perfect squares between 1 and A;
The set of integers between 1 and a.
The number of integers between 1 and a is equal to a. Thus the number of perfect squares between 1 and A is equal to a.

Pisano Period length finding

Can 0 and 1 come together in other positions of the Pisano Period except the first two positions? I am trying to solve a problem where it’s needed to know the Pisano Period length. So I was thinking about searching 0 and 1 in the period.
Yes, if 0 and 1 are adjacent, you're at a point where the sequence is repeating.
A quick proof idea: suppose that you find 0 and 1 adjacent to one another in a Fibonacci sequence mod some number n. In other words, you've found some positions k and k+1 in the sequence such that the kth position is equal to F0 mod n and the (k+1)st position is equal to F1 mod n. That means that position k+2 is equal to F0 + F1 = F2 mod n, and the position after that is equal to F1 + F2 = F3 mod n, etc. This means that if you see 0 and 1 adjacent in the sequence, what follows must be equivalent to the sequence of numbers that you'd find if you started the Fibonacci sequence again from scratch.
Hope this helps!
Here's a quick Python code to determine the Pisano Period
def pisanoPeriod(m):
previous, current = 0, 1
for i in range(0, m * m):
previous, current \
= current, (previous + current) % m
# A Pisano Period starts with 01
if (previous == 0 and current == 1):
return i + 1

Give a big-O bound on the number of bits needed to represent a number N

N is random number,
I am confused with bound.
Any help is appreciated.
Well, for a random number n, there exists a, b such that 2^a <= n <= 2^b or just a k such that 2^(k-1) <= n <= 2^k - 1 (1). We know that for any number less than 2^n, we need log(2^n) = n * log(2) = n bits to represent it (2). For example:
5: 4 < 5 < 8; we need 3 bits for 4, 5 bits for 8 => we need 4 bits for 5
23: 16 = 2^4 < 23 < 32 = 2^5; so we need 5 bits to represent 23
In conclusion, for an exact number of bits b for a random number n, we can use the formula:
b = floor(log(n)) + 1
So, the big-O notation that we are gonna use is O(floor(log(n)) + 1) = O(logn).
Extra info:
SO Answer
Article
1) I supposed it is a random, integer, positive number (although it's easy to generalize for negative numbers too) which I suppose it is your problem case; for fractional numbers is's a bit harder to generalize this formulae
2) The log notation refers to logarithm in base 2

integer divison with negative remainder

I'm trying to find a name for mathematical division operation which can produce negative reminder.
Examples of expected:
5 %? 2 = 2*2 + 1
11 %? 3 = 4*3 - 1
module of reminder should be as small as possible.
Does someone knew the name of such operation?
Computer language remainder functions return a positive remainder. What you need is simple. In your second example you want is a remainder of -1.
11 %? 3 = 4*3 - 1
But what you get is a remainder of 2.
11 %? 3 = 3*3 + 2
As I said in the comments, check the remainder 2 against half the divisor 3/2 = 1.5. Since the remainder is greater than half the divisor, subtract the divisor:
2 - 3 = -1
Yes, you will have to store a real number for half the divisor.

Convert binary, octal, hexa FLOATS to decimal "manually" (ex: [135.263]B10 -> [?]B2)

I'm a student in programmation and I have a course called "Informatic Mathematics". In the exercices it's asked to convert floating numbers from decimal, octal, hexadecimal or binary to another base (not necesserly to base 10) and keep 12 digits after the comma(or the dot) if it is possible. For exemple:
(135.263)b10 => b2
(100101001.11)b2 => b10
(1011110010.0101)b2 => b8
...
I know how to convert numbers. The way I convert the decimal part (after the dot) is to divide this part by the highest multiple of the target base until I get 0 or until I reach the 12th digits after the dot. The problem is that I don't know all the negate multiples of 2 so usually I write them on a separate sheet, but usually I don't have to keep 12 digits after the dot and writing these multiples on a seperate sheet takes time and during the exam, time is a precious thing and I can't waste it to write these multiples.
So I would like to know if there's a better way to do these conversions or if anyone has any tips.
Also, when I convert from non-decimal number to another non-decimal number (ex: b2 => b8) I usually convert the first number to base 10 and then convert the base 10 number to the target base. I would like to know if there's a way to convert the first number directly into the target base without having to convert it in base 10 first.
BTW: Sorry if my english is a bit weird. I'm a french canadian and I did my best, but please let me know if there is something you do not understand well.
I'll start with b2 > b8.
001 011 110 010.010 100
As you see, I've separated the number into 3 digit segments (2^3 = 8). You have to add extra 0 to the left and to the right to make it like that. Then you convert it digit by digit. In this case you'll receive 1352.24
b2 => b10
Some harder math here. Mark digits in your number this way:
1 0 0 1 0 1 0 0 1 . 1 1
8 7 6 5 4 3 2 1 0 -1 -2
Then calculate fractional and whole part
2^0 + 2^3 + 2^5 + 2^8 + 2^-1 + 2^-2
b10 => b2
Multiple the fraction by 2 till you get 1. From each multiplication you take the whole part. Example:
0.25 * 2 = 0.5; 0.5 * 2 = 1;
Thus, 0.25 is 0.01;
UPD For negative conversions check out first and second complement.

Resources