Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

When you have negative number that you are representing in 2\'s complement you n

ID: 3675983 • Letter: W

Question

When you have negative number that you are representing in 2's complement you need to negate the binary representation. In class, we've been flipping the bits and adding one. Another method is 1. Find the farthest right 1 in the bits Flip every bit to its left (i.e., turn Os into Is and Is into Os) Try this method on paper to convince yourself that it works. Now you are going to write a MATLAB function called negateTwosComp that takes as input a vector of length eight containing Os and Is representing an 8 bit signed integer in twos-complement. The function should return a vector of eight Os and Is representing the negation of the input. As an example, negateTwosComp([0, 0, 0, 0, 0,0. 1,1]) should return [1.1,1,1,1,1.0,1] since this is -3 in base-10 and is the negation of the input. You should use conditionals to handle the cases where the input is all 0s (this is zero so does not need to be converted), not length 8. or contains values other than 0s and Is. (Hint: one way to flip a bit is to multiply by -1 and add 1) Turn in negateTwosComp. m

Explanation / Answer

Answer:

function [neg]=negateTwosComp(input)
   neg=zeros(1,8);
   if length(input)<8  
   end
   if all(input=0)
       neg=input;
   end
   if(!any(input=0) ||!any(input=1))
   end
   for k=1:8
       index=k;
       t=input(k);
       if t==1          
           break;
       end
   end
   neg=input;
   for k=1:index
       neg(k)=input(k)*(-1)+1;
   end
   disp(neg);
end
input=[0 0 0 0 0 0 1 1];      
negateTwosComp(input);

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote