Re: Python or PureBasic

Jonikster, you need to understand that BGT, indeed, does have and does use mathematics. Do you know why colleges around the world require that you know your math really well before they will even allow you to join a computer science class? It's because everything you do requires math. Every single program that you right (except, perhaps, the simplest programs that just do input and output) require some form of mathematics, whether it be number base conversion, game point calculations, etc. Hell, even bitwise operations uses math!
Have you ever seen something like the following?
(This program contains a routine to perform a bitwise AND, OR, and XOR on two integers, a bitwise NOT on the first integer, a left shift, right shift, right arithmetic shift, left rotate, and right rotate. All shifts and rotates are done on the first integer with a shift/rotate amount of the second integer.)
PureBASIC style:
Procedure Bitwise(a, b)
  Debug  a & b      ; And
  Debug a | b       ;Or
  Debug a ! b       ; XOr
  Debug ~a          ;Not
  Debug a << b      ; shift left
  Debug a >> b      ; arithmetic shift right
  ; Logical shift right and rotates are not available
  ; You can of use inline ASM to achieve this:
  Define Temp
  ; logical shift right
EnableAsm
  !mov edx, dword [p.v_a]
  !mov ecx, dword [p.v_b]
  !shr edx, cl
  !mov dword [p.v_Temp], edx
  Debug Temp
  ; rotate left
  !mov edx, dword [p.v_a]
  !mov ecx, dword [p.v_b]
  !rol edx, cl
  !mov dword [p.v_Temp], edx
  Debug Temp
  ; rotate right
  !mov edx, dword [p.v_a]
  !mov ecx, dword [p.v_b]
  !ror edx, cl
  !mov dword [p.v_Temp], edx
  Debug Temp
DisableAsm
EndProcedure
And here's that same program, in Python 2.x:
def bitwise(a, b):
        print 'a and b:', a & b
        print 'a or b:', a | b
        print 'a xor b:', a ^ b
        print 'not a:', ~a
        print 'a << b:', a << b # left shift
        print 'a >> b:', a >> b # arithmetic right shift
Here's Python 3.x style:
Python does not have built in rotate or logical right shift operations.
Note: Newer Python versions (circa 2.4?) will automatically promote integers into "long integers" (arbitrary length, bounded by available memory). This can be noticed especially when using left shift operations. When using bitwise operations one usually wants to keep these bounded to specific sizes such as 8, 16, 32 or 64 bit widths. To do these we use the AND operator with specific values (bitmasks). For example:
# 8-bit bounded shift:
x = x << n & 0xff
# ditto for 16 bit:
x = x << n & 0xffff
# ... and 32-bit:
x = x << n & 0xffffffff
# ... and 64-bit:
x = x << n & 0xffffffffffffffff
We can easily implement our own rotation functions. For left rotations this is down by ORing the left shifted and masked lower bits against the right shifted upper bits. For right rotations we perform the converse operations, ORing a set of right shifted lower bits against the appropriate number of left shifted upper bits.
def bitstr(n, width=None):
   """return the binary representation of n as a string and
      optionally zero-fill (pad) it to a given length
   """
   result = list()
   while n:
      result.append(str(n%2))
      n = int(n/2)
   if (width is not None) and len(result) < width:
      result.extend(['0'] * (width - len(result)))
   result.reverse()
   return ''.join(result)
 
def mask(n):
   """Return a bitmask of length n (suitable for masking against an
      int to coerce the size to a given length)
   """
   if n >= 0:
       return 2**n - 1
   else:
       return 0
 
def rol(n, rotations=1, width=8):
    """Return a given number of bitwise left rotations of an integer n,
       for a given bit field width.
    """
    rotations %= width
    if rotations < 1:
        return n
    n &= mask(width) ## Should it be an error to truncate here?
    return ((n << rotations) & mask(width)) | (n >> (width - rotations))
 
def ror(n, rotations=1, width=8):
    """Return a given number of bitwise right rotations of an integer n,
       for a given bit field width.
    """
    rotations %= width
    if rotations < 1:
        return n
    n &= mask(width)
    return (n >> rotations) | ((n << (width - rotations)) & mask(width))
In this example we show a relatively straightforward function for converting integers into strings of bits, and another simple mask() function to create arbitrary lengths of bits against which we perform our masking operations. Also note that the implementation of these functions defaults to single bit rotations of 8-bit bytes. Additional arguments can be used to over-ride these defaults. Any case where the number of rotations modulo the width is zero represents a rotation of all bits back to their starting positions. This implementation should handle any integer number of rotations over bitfields of any valid (positive integer) length.
And believe me, you'll use bitwise operations at some point. I hope that this post shows you the importance of mathematics in computer science.
References:
Bitwise operations - Rosetta Code. (n.d.). Retrieved June 7, 2016, from http://rosettacode.org/wiki/Bitwise_operations
/references

_______________________________________________
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — Developers room : jonikster via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Hrvoje via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Hrvoje via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : jonikster via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Hrvoje via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : jonikster via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : jonikster via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : thggamer via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : jonikster via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : jonikster via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : jonikster via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : GhorthalonTheDragon via Audiogames-reflector

Reply via email to