I need Help with twos Complement
Hi Guys i need to write a program that transforms sbyte numbers in twos complement. Do you have any ideas on how to solve this problem?
36 Replies
We won't do you homework for you. Please elaborate on what you have tried so far and we will try to guide you based on that
what does your current code look like?
can i send you my program? it does weird things.
you can send it in this chat here
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/i did use paste.mod.gg
can you please check it and if you know say what is wrong. Thank you
you need to share the link
BlazeBin - ainzxwelbkan
A tool for sharing your source code with the world!
Did it work?
yep
i don't speak german though
it doesnt really matter
they are just explanations
those are important when i'm trying to find one issue in an entire program 😛
i can translate it
wait a minute
i don't understand the goal of the code, could you explain what the input and output is supposed to be?
the input is a sByte number
the program uses the decimal number and turns it into twos complement
so you have a number and you just want the binary representation as output?
where does the twos complement come in? that's an implementation detail of signed integers, it's not something you typically do by hand
if you want the negative of a number you just multiply by -1 or negate it
the task of the duty is to convert a decimal number in twos complement. He didn´t teach us how to convert it so i tried doing it by hand but it doesnt work.
that's what i'm saying, it doesn't make sense because the computer already does that
because that's how computers handle integers
i think he wants us to explain how the twos complement works by converting it step by step
If you cant help me no problem. I don´t know what he wants from me either.
i mean, the manual process is pretty simple it just doesn't make sense to write a program to do it
Can you show me the manual process? Would be really nice.
to convert between positive and negative you just flip the bits then add 1
for example
1 -> 00000001
flip bits -> 11111110
add 1 -> 11111111
and to convert back to positive you just do the same thing again
do you know a command to flip the bits?
yes, bitwise NOT
var flipped = ~value;
y = ~x + 1
is functionally identical to y = x * -1
maybe if they did this manually on a string representation of a number it would make sense, it would be educative i guess
i did this by hand in college
it makes sense that way, but a computer is already handling numbers in their binary representation anyway
had to do IEEE 754 by hand too
that's a lot of bit juggling
i think i am just dumb. It just doesn´t want to work.
what isn't working?
I don´t even know.
Are there any other methodes to sort it by hand?#
Jimmacle
for example
1 -> 00000001
flip bits -> 11111110
add 1 -> 11111111
Quoted by
<@901546879530172498> from #I need Help with twos Complement (click here)
React with ❌ to remove this embed.
this is how you do it by hand
how to convert a decimal number in bits
ok thank you