Re: Perl hex substraction
by Ratazong (Monsignor) on Feb 15, 2011 at 12:45 UTC
|
Nice problem! I will add this to my list of exercises I use when teaching programming...
I assume your current lesson is loops. Which kind did you already learn about?
(It would be too embarrassing if we would advise you some syntax which your instructor didn't already cover, wouldn't it?).
The arithmetic shouldn't be a problem at all ... remember the hex-function?
Thinking twice, it would probably best if you just show us the code you have already written - and where you have doubts...
that way we could help you best, without wasting your (and our) time ...
Have fun learning!
Rata
| [reply] |
|
|
It is kind of a fun problem, because on the surface the student may think he/she needs to implement a whole new form of subtraction for hex numbers... rather than doing the easy thing which is to convert a hex string to a value, and then just print out the result as a hex value. I can see students learing something like operator overloading in a C++ class getting really wrapped around something like this, trying to create a whole new form of subtraction... in Perl, not nearly the same temptation and underscores the importance of referencing strings as values when you need values, and strings when you need strings.
Then again I remember in an earlier EE course, we were supposed to know how to do hexadecimal subtraction, when we were learning different number bases, longhand... which isn't that tough, especially if you can use 2's complement binary lookups.
| [reply] |
Re: Perl hex substraction
by ELISHEVA (Prior) on Feb 15, 2011 at 12:59 UTC
|
| [reply] [d/l] [select] |
Re: Perl hex substraction
by Corion (Patriarch) on Feb 15, 2011 at 12:35 UTC
|
So, what code have you written, and how does it fail to produce the output you want?
| [reply] |
|
|
The problem is I don't know how to do it :)
I have a file with all those hex numbers like I said: 4d 2c 07 2c 77 b3 77 b3 74 81 9d 59 9d 5f 5a 9e 50 94 4e 8a 4e 8a 4e 8c 43 3c 28 1f 1d 5a 3b fe 3a f1 59 c0 9d f3 54 e2 b2 82 da 68 c1 9b ef bf 9d 59 92 a0 2a 6d 29 ed 29 ed 29 ed 29 ed 29 ed 20 e4 13"
The "decrypted" result of the file is every hex number is the result of substracting from itself the next hex number.
| [reply] |
|
|
| [reply] |
|
|
|
|
|
|
|
|
|
|
inkubus:
Based on your nodes -- above and below -- I suspect you really, really need to read On asking for help and How do I post a question effectively?.
In fact, reading the next few sentences may illuminate your quest:
You'll get better answers here if you show that you've made some legitimate effort to solve your own problem. IOW, show us some code and explain how it fails and what error messages and warnings you see. And if you know nothing about programing, at least answer questions like Corion's -- write up a (structured) little list of the steps by which you could solve your problem with pencil and paper (for a liberal definition of "pencil and paper").
The above answer doesn't do that. But the balance of your interactions in this thread might have gone better had you answered something like this:
- Subtract the second number from the first; save the difference somewhere
- Subtract the third number from the second; save the difference...
- and so on... until...
- Subtract ZERO from the last number and save the difference (or, phrased another way, 'simply save the last number').
That's a simple-minded (and only partial) algorithm to address your request; translating that to code in any language is (just) a matter of matering the language.
And we're here to help when some aspect of the language stumps you... but we're not here to write your code.
| [reply] |
Re: Perl hex substraction
by ikegami (Patriarch) on Feb 15, 2011 at 17:53 UTC
|
Hex is a representation of a number. In order to do math, one must first convert it to something the computer considers a number. «hex» can do that.
You want to do 8 bit arithmetic while ignoring overflows. No problem, just filter out all but the lower 8-bits using «&».
You will need to convert the result back to hex. «sprintf "%02x"» will do that.
| [reply] |
Re: Perl hex substraction
by cdarke (Prior) on Feb 15, 2011 at 13:20 UTC
|
07 - 2c = db
No it is not, it is negative.
In decimal, 7 - 44 = -37
37 in hex is 0x25. However -37 is represented as 0xffffffffffffffdb
I get the following results: 0x4d - 0x2c = 0x21 (decimal: 33)
0x2c - 0x07 = 0x25 (decimal: 37)
0x07 - 0x2c = 0xffffffffffffffdb (decimal: -37)
0x2c - 0x77 = 0xffffffffffffffb5 (decimal: -75)
0x77 - 0xb3 = 0xffffffffffffffc4 (decimal: -60)
0xb3 - 0x77 = 0x3c (decimal: 60)
0x77 - 0xb3 = 0xffffffffffffffc4 (decimal: -60)
| [reply] [d/l] |
|
|
| [reply] |
|
|
Yes. Either way, it is certainly not db.
| [reply] |
|
|
all those f's are the sign extension, which will vary according to word size. if you had big ints, it'd be even bigger. :)
| [reply] |
|
|
I know, but this is not my problem.
Let's assume I have the following array:
@names = ("Ann","John","Michael","George","Smith");
What I want to accomplish is to print them in pairs like:
Ann John
John Michael
Michael George
George Smith
Every object with the next one to it in the list...simple, but I don't know how to do that. | [reply] [d/l] [select] |
|
|
|
|
|
|
|
|
|
|
|