View previous topic :: View next topic |
Author |
Message |
RustyWaffle
Joined: 15 Apr 2007 Posts: 796
|
Posted: Mon Sep 17, 2012 12:20 am Post subject: |
|
|
Sarg338 wrote: | RustyWaffle wrote: | Can anybody here help me in Two's Compliment Arithmetic? I'm taking a course and I am absolutely dumbfounded by this having little programming background. My first two problems are subtraction in base 10 & base 2. |
Had to do this in networking, and once you understand it, it's not that hard.
From Here:
Quote: | To calculate the 2's complement of an integer, invert the binary equivalent of the number by changing all of the ones to zeroes and all of the zeroes to ones (also called 1's complement), and then add one.
For example,
0001 0001(binary 17) 1110 1111(two's complement -17)
NOT(0001 0001) = 1110 1110 (Invert bits)
1110 1110 + 0000 0001 = 1110 1111 (Add 1) |
When working with a Signed integer, the leftmost bit tells you whether it's positive or negative. If it's 0, then it's equal to or greater than 0. If it's 1, then it's a negative number.
2's complement is just used to flip a positive number to it's negative binary counterpart (and vice versa). |
After an hour or so of Google, this is what I came up. Let me know if there's anything I did wrong. _________________
|
|
Back to top |
|
|
Sarg338
Joined: 07 Feb 2008 Posts: 5143
|
Posted: Mon Sep 17, 2012 12:46 am Post subject: |
|
|
Just as a quick rundown of the conversion, b is wrong. Your work is correct, just misadded the numbers.
C is also wrong. 2 doesn't divide into 61 evenly.
A looks correct. If it's not due until tomorrow or something, I can look at your Two's Complement stuff later if no one else does.
EDIT: Also, a good thing to know is that the calculator that comes with Windows (If you're running it) can convert base2/8/10/12 perfectly (It's still good to KNOW how to do it of course). Just open it up, View -> Programmer mode. If you want to go from Base-10 to Base-2, type in a number as usual, then click the Bin bubble. _________________
|
|
Back to top |
|
|
RustyWaffle
Joined: 15 Apr 2007 Posts: 796
|
Posted: Mon Sep 17, 2012 1:10 am Post subject: |
|
|
I.
a.
Complement of 349 = 999 - 349 = 650
+ 1 = 651
787 + 651 = 1438
Take away the high order digit, and the answer is 438.
b.
11011 - 1100
Zero fill subtrahend to match the minuend
11011 - 01100
Reverse the digits of the subtrahend, obtain 10011
11011 + 10011 = 0101110
Remove the high order number, and the answer is 101110
II.
a.
10|1100|1111
0010 = 2
1100 = C
1111 = F
2CF
b.
110101
1*2^5 + 1*2^4 + 0*2^3 + 1*2^2 + 0*2^1 + 1*2^0
32 + 16 + 0 + 4 + 0 + 1
53
c.
495
495/2 = 247 R1
247/2 = 123 R1
123/2 = 61 R1
61/2 = 30 R1
30/2 =15 R0
15/2 = 7 R1
7/2 = 3 R1
3/2 = 1 R1
111101111 base 2
This is what my revisions come to. I noticed the few minor errors which only seemed to be simple miscalculations on my part. It's due by 10 PM though lol _________________
|
|
Back to top |
|
|
Vampyromaniac
Joined: 08 Feb 2008 Posts: 1216
|
Posted: Thu Oct 25, 2012 3:20 am Post subject: |
|
|
double a = 1;
if (1 != 0){ a = (1/2);}
else {a = (1/2);}
System.out.println(a);
WHY does this output 0?
The above code is Java, but this issue was originally ruining my Maple code; I just translated it for debugging, and narrowed the source of the problem (the problem of the source) to this weird anomaly. This exact example obviously has nothing to do with my original intent. If we replace 1/2 with .5, it works, but that doesn't help me much because my original code was trying to create polynomials but replacing arbitrary variables with 0. _________________
[/u] |
|
Back to top |
|
|
Sarg338
Joined: 07 Feb 2008 Posts: 5143
|
Posted: Thu Oct 25, 2012 4:09 am Post subject: |
|
|
Vampyromaniac wrote: | double a = 1;
if (1 != 0){ a = (1/2);}
else {a = (1/2);}
System.out.println(a);
WHY does this output 0? |
It's doing what's called "Integer division", which truncates everything to the right of the the decimal. It's basically saying "How many 2's can you fit in a 1? 0". If you say "Double a = 1.0/2.0", you'll get .5 as the answer!
I'm actually having some trouble with OpenMP if anyone knows the basics of that and could help me out! _________________
|
|
Back to top |
|
|
CoolMotion
Joined: 16 May 2007 Posts: 1097 Location: Ohio, USA
|
Posted: Thu Jan 31, 2013 4:03 am Post subject: |
|
|
Well, I have a semi-programming question that I could not find the answer to anywhere, and I knew that the Programming thread on Scorehero is still kind of active, so here I am .
What I'm trying to do is make my flash drive a nice little portable C++\Java editor and compiler, using Notepad++ and such. I have the C++ all figured out (using NPPExec and minGW compiler), but Java is giving me problems.
I have the Java data (the JDK and the JRE files required) on the flash drive, the problem is changing the computer's environment variables from the flash drive. After researching around I found something that involved a batch file, which seemed to me like it would work, but for some reason I just can't change em from a bat file. I can use the "SET" command on the flash drive just fine (environment variables are displayed and such), but I can't change them. My batch file is just a single line of code:
Code: | set PATH=E:\Java\jdk1.7.0_11\bin;%PATH% |
Also this doesn't just not work from the flash drive, it just plain doesn't work.
Is it the code? Something to do with privileges? Any help is appreciated.
EDIT: Forgot to mention that I don't know much about using bat files and couldn't honestly tell you anything about shell script. That line was copypasta'd with a tweak I had to make for the filepath. _________________
Back to playing rhythm games.
Check out my music taste: last.fm |
|
Back to top |
|
|
conlan22
Joined: 25 Aug 2007 Posts: 1542
|
Posted: Tue Jan 28, 2014 11:15 pm Post subject: |
|
|
CoolMotion wrote: | Well, I have a semi-programming question that I could not find the answer to anywhere, and I knew that the Programming thread on Scorehero is still kind of active, so here I am .
|
guess not lol. Anyways, I had to take a programming class as part of majoring in mathematics and the experience totally turned me off. Recently I have been getting back into it and learning things myself and I am having much more fun. I think the teacher I had in my CSci class kinda turned me off to programming but now that I am back into it I wish I would have gone for a minor or possibly double major with math. |
|
Back to top |
|
|
CoolMotion
Joined: 16 May 2007 Posts: 1097 Location: Ohio, USA
|
Posted: Thu Jan 30, 2014 6:47 pm Post subject: |
|
|
Oh wow I had totally forgot I was trying to do that. That was a year ago? Damn... _________________
Back to playing rhythm games.
Check out my music taste: last.fm |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Copyright © 2006-2024 ScoreHero, LLC
|
Powered by phpBB
|