Results 1 to 6 of 6

Thread: python->pascal

  1. #1
    Senior Member
    Join Date
    Jul 2003
    Posts
    166

    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 ?!
    Code:
     result = ''
    What this means? And in nextDecode result is integer.
    Code:
    arg1 & 1 << i != 0
    What does this mean?
    And where is the main function of this ?! ... or it hasn't
    Last edited by Barov4e; January 13th, 2008 at 03:51 PM.
    BGDevS
    [gloworange]www.peaksoft.info [/gloworange]

  2. #2
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    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

  3. #3
    Senior Member
    Join Date
    Jul 2003
    Posts
    166
    Code:
    result = result | 1 << i
    I understan this, but how can I realize it in Pascal. Is there some sort of function?
    BGDevS
    [gloworange]www.peaksoft.info [/gloworange]

  4. #4
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    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

  5. #5
    Senior Member
    Join Date
    Jul 2003
    Posts
    166
    Thanks, I'll check it. I just didn't think of searching for pascal bitwise operators
    BGDevS
    [gloworange]www.peaksoft.info [/gloworange]

  6. #6
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    Don't worry about it - there's nothing wrong with asking questions, I just got a bit cranky.

    ac

Similar Threads

  1. Python 2.5 Released
    By HTRegz in forum General Computer Discussions
    Replies: 0
    Last Post: September 19th, 2006, 07:28 PM
  2. Developing a Port Scanner in Python
    By HTRegz in forum The Security Tutorials Forum
    Replies: 12
    Last Post: January 28th, 2006, 08:02 PM
  3. Python Introduction
    By HTRegz in forum Other Tutorials Forum
    Replies: 20
    Last Post: September 10th, 2005, 07:28 AM
  4. Beginning Network Programming in Python
    By PacketThirst in forum Other Tutorials Forum
    Replies: 2
    Last Post: August 14th, 2005, 07:16 PM
  5. Basic Python Tutorial for Newbies
    By [pHA]XeNoCiDe in forum AntiOnline's General Chit Chat
    Replies: 11
    Last Post: July 8th, 2002, 07:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •