-
python->pascal
Hi guys,
I have a problem converting a very simple software from python to pascal. I haven't use python before and that is the reason I can't understand some codes. I am attaching the source. But here is what I can't understand:
Code:
def nextDecode(arg1):
global index
result = 0
for i in range(arg1):
rc = shouldDecodeStep(index)
if (rc == -1):
break
else:
if rc:
result = result | 1 << i
index += 1
return result - (1 << arg1 - 1)
First, which is the body of "for" loop? And what does this:
Code:
result = result | 1 << i
Code:
try:
val1 = textbuffer[lindex / 7]
val2 = lindex % 7
return (val1 >> val2 & 1 == 1)
except Exception, e:
return -1
I suppose this is try/except in pascal ?!
What this means? And in nextDecode result is integer.
What does this mean?
And where is the main function of this ?! ... or it hasn't
-
Bear in mind I don't know python, but as far as I know python is whitespace sensitive. That would lead me to believe that the following is the body of the for loop:
Code:
rc = shouldDecodeStep(index)
if (rc == -1):
break
else:
if rc:
result = result | 1 << i
index += 1
As for the following:
Code:
result = result | 1 << i
| is normally the bitwise or operator, and << generally means shift the left side of the operator by the value on the right side. To understand what it does you need to know the precedence of the operators. i.e. does it mean:
Code:
result = (result | 1) << i
or
result | (1 << i)
Assuming the former, here's an example of what it does:
Code:
result = 5 (0101)
i = 1 (0001)
5 | 1 = 5
0101
0001
0101
5 << 1 = 10
1010 (all bits have moved left one place)
Hope that helps a bit.
ac
-
Code:
result = result | 1 << i
I understan this, but how can I realize it in Pascal. Is there some sort of function?
-
Sorry, I misunderstood. If you do a search on google for "pascal bitwise operator" the first link takes you to a page with explanations of the bitwise operators. Maybe try doing a search next time before posting questions.
To answer your question, I believe the following will do what you want (assuming pascal and python have the same rules for operator precedence):
Code:
result := result or 1 shl i;
Don't quote me on it though since it's been years since I've even looked at pascal. You should search for a pascal tutorial or something because you don't seem to understand the basics.
ac
-
Thanks, I'll check it. I just didn't think of searching for pascal bitwise operators :(
-
Don't worry about it - there's nothing wrong with asking questions, I just got a bit cranky.
ac