Convert a negative number with a fraction in 2's complement [closed] - binary

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I want to convert number -0.25. So 0.25 (for 4 bit int and 4 bit frac) equals to:
0000_0100
For negative number -1 answer is
1111
But what is -0.25? How can I convert this negative number with a fraction in 2's complement?

To answer this question, I need to clarify that the numbers are represented in a fixed-point. Almost all logic nowadays uses two's complement. Therefore, in the two's complement version negative numbers are considered for example:
signed 4'b1100 = -4 (-1*2^3 + 1*2^2 + 0*2^1 + 0*2^0).
Number for example 0.25 in fixed-point binary will be represented:
8'b0000_0100 (int 4 bit is zeros and frac 4 bit -> 0*2^-1 + 1*2^-2 + 0*2^-3 + 0*2^-4).
Other way to calculate can be multiply the number to integer find negative and then divide this number (remember, that multiplication on 2 is shift to the right, and division on 2 is shift to the left).
For my example -0.25 would be as follows:
-0.25*2*2= -1.
-1 = 4'b1111.
shift 2*2 to left = 4'b11_11 = -0.25.
Or we can calculate: -1*2^1 + 1*2^0 + 0*2^-1 + 1*2^-2.
1 in MSB is signed bit. We must remember this when operate with signed logic.

Related

Prove that not all ternary linear codes contain the all zero codeword [closed]

Closed. This question is not about programming or software development. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 days ago.
Improve this question
I have to prove that not all ternary linear codes contain the all zero codeword.
Some term explanations:
A linear code is a code that the sum or difference of any two codewords must also be a codeword. In other words, for all x, y in C, x +/- y is also in C.
A ternary codeword in this exercise is a codeword containing 0, 1 or 2. Eg, 1011, 1012, 012, ...
An all zero codeword is something like 0000, 000, 00000, ...
My attempt:
The addition of codewords must be done MOD 3 which means there are three allowable numbers in this MOD 3 world including 0, 1 and 2. For any ternary linear codes, there are cases when all the additions of any two codewords yeild non-zero codewords.
I am not sure about the proof. I have an idea that it must deal with the MOD 3 but not sure how to explain.
Thank you so much!

I have a question regarding a signed binary calculation

Suppose I have two signed binary which is:
A: 1100
B: 1000
And I want to subtract B from A.
After 2's complementing B, this would be:
1100 + 1000 = '1'0100
I thought that there would be an overflow from this because the sign for A and B is negative while the answer is positive, but my worksheet answer key says otherwise.
My question is, will the overflow be 1 or is the answer key mistaken?
There's no overflow in this computation.
1100 is -4 decimal.
1000 is -8 decimal.
-4 - (-8) = 4
So the result is 0100, without any overflow. (All the values involved are representable as signed-4-bit, which covers the range -8 to 7 inclusive.)
Note that subtraction never overflows (neither overflow, nor underflow) if the operands have the same sign, which is the case here.
It appears you're confusing the extra bit at the end of the addition with determining whether overflow happened. This simply isn't the case in general; for each operation there are different rules to determine if an overflow/underflow happened. Here's a good paper to read, which details the exact conditions under which underflow/overflow occurs for signed and unsigned binary arithmetic of bit-vectors: https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/z3prefix.pdf

How do I convert 234.35 into Binary? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I need to convert the above into Binary. I'm at a loss here.
Can you include full information on how you got the answer too?.
Step by step would be great!
First, convert the integral part which is 11101010. Next, you build the decimal part. If demonstrated informally, it is something like:
0.1 is one half, but 0.35 is less than that, so the first binary digit is zero -> .0 (0.35)
0.01 is one quarter, and 0.35 is greater than that, so then follows one. 0.35 minus one quarter is decimal 0.1 -> .01 (0.35 - 0.25 = 0.1)
0.001 is one eighth, and decimal 0.1 is less than that, so again zero -> .010 (0.1)
Next steps are: .0101 (0.1 - 0.0625 = 0.0375); .01011 (0.0375 - 0.03125 = 0.00625); then follow two zeroes, and we can probably cut it here.
So, the answer is 11101010.010110... As the original number has decimal part of 7/20, it becomes infinite when converted to binary.

How to store longitude and latitude in database. MySQL won't go further than -99.9999999 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Ok right now I'm storing lat and lng columns as DECIMAL(10,8)
I'm trying to insert this:
-117.1779216 However it keeps inserting as this : -99.99999999
Why is this? I see every other board storing it as DECIMAL, but it won't go back more when I'm giving it 10 places before the period... So I would think it could go -117.. None of them are marked as unsigned either.
Precision (10) - Scale (8) = 2
2 is the number of digits you can have to the left of the decimal
If you increase the precision, you can have more digits to the left of the decimal.
The declaration syntax for a DECIMAL column is DECIMAL(M,D). The ranges of values for the arguments in MySQL 5.1 are as follows:
M is the maximum number of digits (the precision). It has a range of 1 to 65. (Older versions of MySQL permitted a range of 1 to 254.)
D is the number of digits to the right of the decimal point (the scale). It has a range of 0 to 30 and must be no larger than M.
Taken from the manual :D
You set it to DECIMAL(10,8) with the number -117.1779216. MySQL reads this as -117.17792160 because it needs 8 digits of precision, but you said it should only have 10 digits, so anything over 99 makes it 11 digits and invalid.

Java Code Explain [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I got this code somewhere from the internet
final int time = 80 << 3 + 1;
<< is a left shift operator
The signed left shift operator "<<" shifts a bit pattern to the left,
and the signed right shift operator ">>" shifts a bit pattern to the
right. The bit pattern is given by the left-hand operand, and the
number of positions to shift by the right-hand operand.
So 24 << 8 means shift binary value of 24 towards left by 8 bits position.
Follow reference to learn more about it.
24 << 8 means shifting the number 24 to the left for 8 bits, which is equivalent to 24 * (2^8) = 6144.
In the provided code snippet, it encodes a time hh:mm into an integer hh << 8 + mm.
Since there are 24 hours in one day, the array to represent the activity of every minute in one day requires (24 << 8) + 1 elements. The +1 is to make the array index for 24:00 legal.
It means bit-shift the constant integer value 24 left by 8 bits.