IWETHEY v. 0.3.0 | TODO
1,095 registered users | 1 active user | 1 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New According to my text
they should be equivalent.

Anyhoo... this seems hacky at best:

sum = value1 + value2;\n\nif (value1 < 0)\n        outval1 = value1 - 0xFFFFFF00;\nelse\n        outval1 = value1;\nif (value2 < 0)\n        outval2 = value2 - 0xFFFFFF00;\nelse\n        outval2 = value2;\n\nif (sum < 0)\n        outsum = sum - 0xFFFFFF00;\nelse\n        outsum = sum;\n\n\nprintf("%x + %x = %x, sum = %d, v = %d\\n", \n        outval1, \n        outval2, \n        outsum, \n        sum, \n        (sum < -128 || sum > 127));
The idea being that I have to represent a negative value as an eight bit value, minus the leading 'FFFFFF' that's in an int in two's complement form. Further, I have to output the sum as a decimal value, though there are no conditions on the range. I also have to put out the value of the carry bit as it would exist if it were an eight bit signed value, hence the truth value if it falls outside the range.

Anyone who can see a more elegant way to do this?
--\n-------------------------------------------------------------------\n* Jack Troughton                            jake at consultron.ca *\n* [link|http://consultron.ca|http://consultron.ca]                   [link|irc://irc.ecomstation.ca|irc://irc.ecomstation.ca] *\n* Kingston Ontario Canada               [link|news://news.consultron.ca|news://news.consultron.ca] *\n-------------------------------------------------------------------
New use "unsigned char"
..which goes 0..255 instead of char (or short or whatever) which goes -128..127.

I checked and < and >, || do have different precedence but || is *lower* than < and > so your grouping should have had no effect :/

When comparing things of different type, use a cast if you want to be certain you are comparing things of the same type, i.e.

int A = 258;
if ((unsigned char) A == 2)
puts("Whoa\\n");
-drl
New Re: According to my text
Something along the lines of:

\n#include <stdlib.h>\n#include <stdio.h>\n\nint main(int argc, char **argv)\n{\n  unsigned char v1, v2, sum;\n  unsigned int largeSum;\n \n  v1 = atoi(argv[1]);\n  v2 = atoi(argv[2]);\n  \n  sum = v1 + v2;\n  largeSum = (unsigned int)v1 + (unsigned int)v2;\n  \n  printf("%02X + %02X = %02X (carry is %d)\\n", \n\t (unsigned int)v1, (unsigned int)v2,\n\t (unsigned int)sum, (largeSum & 0x100) != 0);\n}\n  \n\n


Here is the output
\nC:\\cygwin\\home\\Arkadiy>.\\test 12 13\n0C + 0D = 19 (carry is 0)\n\nC:\\cygwin\\home\\Arkadiy>.\\test 255 255\nFF + FF = FE (carry is 1)\n\nC:\\cygwin\\home\\Arkadiy>.\\test 255 254\nFF + FE = FD (carry is 1)\n\nC:\\cygwin\\home\\Arkadiy>.\\test 126 127\n7E + 7F = FD (carry is 0)\n\nC:\\cygwin\\home\\Arkadiy>.\\test 127 127\n7F + 7F = FE (carry is 0)\n\nC:\\cygwin\\home\\Arkadiy>.\\test 127 128\n7F + 80 = FF (carry is 0)\n\nC:\\cygwin\\home\\Arkadiy>.\\test 128 128\n80 + 80 = 00 (carry is 1)\n


Commentaries: unsigned char is used for arithmetics as it gives you modulo 255 for free. To compute carry, I use unsigned int (thus preserving the carry) and test for the value of the ninth bit using bitwise and (signed int would work too).

Note how unsigned char is cast to unsigned int in printf. I think it's necessary, because printf uses ellipsis (...) and ellipsis converts anything smaller than int to int. In the process, it may cause sign expansion (converting 0xFF to 0xFFFFFFFF). So , to be safe, we explicitly tell the compiler _how_ to convert to the int size using cast. Since we are casting from unsigned to unsigned, sign expansion does not happen.

It may be that the above is just my paranoia. But better safe than sorry. It's also possible to acheve the same result by simple bitwise anding with 0xFF in the printf statement - bitwise and with an integer converts to integer, and does it properly.

Note that I do not check if argv[1] or [2] are present. Not a good thing to do for real world.
--

OK, George W. is deceptive to be sure. Dissembling, too. And let's not forget deceitful. He is lacking veracity and frankness, and void of sooth, though seemingly sincere in his proclivity for pretense. But he did not lie.
[link|http://www.jointhebushwhackers.com/not_a_liar.cfm|Brian Wimer]
     I'm having a problem grokking C - (jake123) - (61)
         "\\0" is a string - not a character - (ChrisR) - (54)
             Re: "\\0" is a string - not a character - (jake123) - (23)
                 Double quotes is a string: - (ChrisR)
                 You want to use '0' not "0". - (a6l6e6x) - (21)
                     Thanks guys - (jake123) - (20)
                         sheesh, wackiness - (jake123) - (17)
                             operator precedence -NT - (deSitter) - (3)
                                 According to my text - (jake123) - (2)
                                     use "unsigned char" - (deSitter)
                                     Re: According to my text - (Arkadiy)
                             Should be equivalent indeed - (Arkadiy) - (12)
                                 Well, interestingly enough - (jake123) - (11)
                                     Re: Well, interestingly enough - (Arkadiy) - (10)
                                         Yes, I was sure. -NT - (jake123) - (9)
                                             copy/paste the full code, then -NT - (Arkadiy) - (8)
                                                 Sure, here's my final version - (jake123) - (7)
                                                     Works with or without parens for me - (Arkadiy) - (4)
                                                         Feel free - (jake123) - (3)
                                                             Re: Feel free - (Arkadiy) - (2)
                                                                 You're suggesting that - (jake123) - (1)
                                                                     Basically, yes - (Arkadiy)
                                                     Works here too, both ways. gcc 3.3.2 -NT - (scoenye) - (1)
                                                         It is working here - (jake123)
                         What is the type of 'value1' ? - (jb4) - (1)
                             Nah, it's an int - (jake123)
             NOT better! - (jb4) - (29)
                 Think we've had this argument before - (ChrisR)
                 Agreed -NT - (deSitter)
                 NOT boolean - this is C - (tuberculosis) - (25)
                     Re: NOT boolean - this is C - (deSitter) - (1)
                         Nah, put on your binary thinking cap. :) - (a6l6e6x)
                     Yes, yes...agreed - (jb4) - (22)
                         Breaking bad habits - (ChrisR) - (21)
                             Good points... - (jb4) - (15)
                                 Not that I do a tremendous amount of C programming... - (admin) - (4)
                                     K&R - (ChrisR)
                                     My mileage varies. :-) - (static)
                                     They are all ugly - (tuberculosis) - (1)
                                         Yep. - (admin)
                                 The business seems to have plenty of time on its hands - (ChrisR) - (9)
                                     Disagree with several of your premises - (jb4) - (8)
                                         Dude - (tuberculosis) - (7)
                                             Which reminds me - C++ is why our hardware sucks (new thread) - (tuberculosis)
                                             rofl - (deSitter)
                                             Heh - (jake123)
                                             Dude... - (jb4) - (3)
                                                 Whoa, JB gets feisty! - (deSitter)
                                                 Non-sequitur - (tuberculosis) - (1)
                                                     OK, then apology accepted........;-) - (jb4)
                             I'd have to agree - (jake123) - (4)
                                 Used to be a Fortran thing - (ChrisR) - (3)
                                     Re: Used to be a Fortran thing - (deSitter) - (2)
                                         Yep -FORTRAN for math libs rulez - (tuberculosis) - (1)
                                             Re: Yep -FORTRAN for math libs rulez - (deSitter)
                 Misra C guidelines - (ChrisR)
         Ob-C advice - (deSitter)
         Stylistic tip - (ben_tilly)
         Re: I'm having a problem grokking C - (tuberculosis) - (3)
             Hehehe - (jake123) - (2)
                 int isdigit(int c) { return (c >='0' && c <= '9'); } - (tuberculosis) - (1)
                     :) - (jake123)

Hate to have a typo or spelling error made immortal by the LRPD.
98 ms