Please help me create Decimal to Two's Complement/ Two's Complement to Decimal function without using the built-in function in Matlab
Mostrar comentarios más antiguos
Hello Everyone,
My goal here is to create the Decimal to Two's Complement/ Two's Complement to Decimal function without using the built-in function in Matlab , aslo to create hexadecimal number to decimal number and vise versa. without using hex2dec & dec2hex.
Your input is much appreciated.
Thank you.
41 comentarios
James Tursa
el 10 de Dic. de 2018
What have you done so far? What specific problems are you having with your code? Are you working only with integers within a certain range?
James Tursa
el 11 de Dic. de 2018
Editada: James Tursa
el 11 de Dic. de 2018
What is "bineries"? Also, your bit calculations will immediately have problems because doubles do not have 64 bits of precision. I.e., the 2^bits-1 isn't going to give you what you expected if bits = 64.
Walter Roberson
el 11 de Dic. de 2018
What is the purpose of the variable named bin ?
Walter Roberson
el 11 de Dic. de 2018
Editada: Walter Roberson
el 11 de Dic. de 2018
Your current code has
x = [binarynumber] % restrict the inputs betwen 0 and 1 [11010100111.....bin-1]
What is it that you expect that line to do ? It appears to overwrite the input variable, x but we do not know what binarynumber is ? Is it a function ?
Irwin2020
el 12 de Dic. de 2018
Walter Roberson
el 12 de Dic. de 2018
When the line
x = [binarynumber] % restrict the inputs betwen 0 and 1 [11010100111.....bin-1]
is executed, what are you expecting to have happen? What are you overwriting the variable x with ?
If the input to the function had been 9, then what would be the result after executing
x = [binarynumber] % restrict the inputs betwen 0 and 1 [11010100111.....bin-1]
?
If the input to the function had been 9.5, then what would be the result after executing
x = [binarynumber] % restrict the inputs betwen 0 and 1 [11010100111.....bin-1]
?
If the input to the function had been 10, then what would be the result after executing
x = [binarynumber] % restrict the inputs betwen 0 and 1 [11010100111.....bin-1]
?
Irwin2020
el 12 de Dic. de 2018
Walter Roberson
el 12 de Dic. de 2018
We recommend that you do not go back and edit your code without mentioning it. The people volunteering typically assume that the old code is still what you are using unless you specifically post new code or specifically say that you changed it and that we should go back to look.
Walter Roberson
el 12 de Dic. de 2018
For the purpose of your function twosComplement, are the following legal calls ?
twosComplement(11, 8)
twosComplement(12, 8)
twosComplement('101', 8)
twosComplement(uint16(279), 8)
twosComplement(int16(-279), 8)
twosComplement('037f', 16)
Irwin2020
el 13 de Dic. de 2018
Walter Roberson
el 13 de Dic. de 2018
Are we talking about addHexNumber function i.e the old code?
No, we are talking about twosComplement . You failed to documented the permitted inputs, so I asked specific questions, listing potential input values and asking whether each of them is intended to be handled or not.
Is there any inverting method to invert a Hexa char value to a number( Integer) hex2int and vice versa without using Matlab functions??
No. It is not possible to do computation in MATLAB without using MATLAB functions. See https://www.mathworks.com/matlabcentral/answers/38787-what-can-be-programmed-without-any-built-in-functions . If you were to list a set of permitted functions, or alternately you were to list a set of excluded functions, then we could answer in terms of that set.
I would point out that ones-complement can be implemented for hex purely in terms of array lookups. Add-one can also be implemented in hex in terms of array lookups, but with a small bit of logic added. Twos-complement is ones-complement followed by Add-one. Therefore by induction, twos-complement can be implemented in hex in terms of array lookups with a small bit of logic added. You do not need to convert to decimal.
onesc('0123456789abcdef') = 'fedcba9876543210';
OnesComplement = @(TheInputHexCharacterVector) onesc(lower(TheInputHexCharacterVector))
Walter Roberson
el 13 de Dic. de 2018
No code for addHexNumber here.
James Tursa
el 13 de Dic. de 2018
Editada: James Tursa
el 13 de Dic. de 2018
The uint64 and int64 stuff is silly. You already know that the values are in the range 0-15, so what is the point of these conversions?
Irwin2020
el 13 de Dic. de 2018
Irwin2020
el 13 de Dic. de 2018
James Tursa
el 13 de Dic. de 2018
I'd like to back up a bit. Can you simply show us a variety of sample inputs and desired outputs? It is still not clear to me what all you want out of this discussion. Converting hex strings, integers, floating point numbers? To/From what? Etc.
Irwin2020
el 13 de Dic. de 2018
Walter Roberson
el 13 de Dic. de 2018
do not use hex2dec or any numeric representation . hex2dec converts to double precision . Your values overflow uint64.
Walter Roberson
el 13 de Dic. de 2018
When you are dealing with 2's complement, the algorithm for subtraction is the same as adding the 2's complement of the subtrahend. Therefore you do not need any of those special cases: just add the values together in all those cases.
Walter Roberson
el 14 de Dic. de 2018
Write your hex addition routine that works on the character elements without transforming into decimal . Then be consistent about representing in two's complement .
Note that two's complement requires either fixed width or else case analysis to determine whether to expand into more bytes.
Walter Roberson
el 17 de Dic. de 2018
If you were using character vectors instead of strings, then consider
t(1) = dec2hex(hex1(end+1-1)) + dec2hex(hex2(end+1-1)
t(2) = dec2hex(hex1(end+1-2)) + dec2hex(hex2(end+1-2)
and so on. After which you have to process carry, then you convert back to hex, and finally reverse the order.
Reminder: Note that two's complement requires either fixed width or else case analysis to determine whether to expand into more bytes.
Irwin2020
el 19 de Dic. de 2018
Walter Roberson
el 19 de Dic. de 2018
Set carry to 0
Loop starting from the least significant digit (the end)
add the two corresponding hex positions and the carry. If the result is 15 or less set carry to 0, otherwise subtract 16 from the result and set carry to 1.
Continue looping towards more significant digits (the beginning)
After you have looped to add the most significant characters, if the carry is 0 then you are done. If the carry is not 0, then if you are working with unsigned values, extend your strings by a leading 1 (the carry.) If the carry is not zero and you are working with two's complement then you are done without having to extend the strings.
Number of digits at a time that need to be translated to decimal: at most 1. Not 10, not 40.
This is just standard addition logic in any base. When adding two values in any base, the maximum carry is always 1.
Walter Roberson
el 20 de Dic. de 2018
hexval('A':'F') = 10:15;
hexval('a':'f') = 10:15;
hexval('0':'9') = 0:9;
Now
hexval(char(Fs))
looks up each hex byte into its decimal arithmetic without using hex2dec . It is translating only one byte at a time, so for example 'FF' would translate as [15 15] not as [255] . But I explained above why you only need one digit at a time.
hexval(char(Fs1)) + hexval(char(Fs2))
Now process carry from the last towards the beginning.
Walter Roberson
el 20 de Dic. de 2018
hexval(char(Fs1)) + hexval(char(Fs2))
can be right before a for loop that handles carry.
Walter Roberson
el 20 de Dic. de 2018
You posted
function [result] = Clockregister01(R,count)
clc
for j = 1:count
for i = 1:size(RN,1)-1
% RN(i,1:size(RN,2)) = addHexNumber(RN(i,1:size(RN,2)),RN(i+1,1:size(RN,2)))
A = addHexNumber(RN(i,:),RN(i+1,:));
end
end
result = RN;
end
You clear the command window in each call of the function, which is bad user interface.
You ovewrite A in each iteration.
You do not use the result A after you compute it.
You ignore your input R.
You use RN as if it is a variable, but you do not define it in this function and you do not pass it in.
Your code contains no documentation, so it is not possible to tell what the intended functionality is.
Irwin2020
el 20 de Dic. de 2018
Irwin2020
el 20 de Dic. de 2018
Walter Roberson
el 20 de Dic. de 2018
Editada: Walter Roberson
el 20 de Dic. de 2018
function [ result ] = NaddHexNumber( hex1,hex2 )
hexval('A':'F') = 10:15;
hexval('a':'f') = 10:15;
hexval('0':'9') = 0:9;
size = strlength(hex1);
bothHexSum = hexval(char(hex1)) + hexval(char(hex2))
for i = size:-1:2
if bothHexSum(i) > 15
bothHexSum(i) = bothHexSum(i) - 16;
bothHexSum(i-1) = bothHexSum(i-1) + 1;
end
end
if bothHexSum(1) > 15; bothHexSum(1) = bothHexSum(1) - 16; end
Now convert bothHexSum into hex.
Irwin2020
el 20 de Dic. de 2018
Walter Roberson
el 20 de Dic. de 2018
I have corrected the code
Irwin2020
el 24 de Dic. de 2018
Respuestas (0)
Categorías
Más información sobre Downloads en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!