Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does the '~' mean in python? [duplicate]

what does the '~' mean in python?

i found this BF interpreter in python a while ago.

import sys

#c,i,r,p=0,0,[0]*255,raw_input()

c=0   
i=0
p=raw_input()    
r=[0]*255 

while c<len(p):
    m,n,u=p[c],0,r[i]
    if m==">":i+=1
    if m=="<":i-=1
    if m=="+":r[i]+=1
    if m=="-":r[i]-=1
    if m==".":sys.stdout.write(chr(u))  
    if m=="[":
        if ~u:
            while 1:
                m=p[c]
                if m=="]":n-=1
                if m=="[":n+=1
                if ~n:break
                c+=1
    if m=="]":
        if u:
            while 1:
                m=p[c]
                if m=="]":n-=1
                if m=="[":n+=1
                if ~n:break
                c-=1    
    c+=1

and i want to know what it does because i want to make one on my ti 84 (and a PF one)

BF is http://en.wikipedia.org/wiki/Brainfuck and PF is something similar

like image 289
hidroto Avatar asked Jun 12 '10 03:06

hidroto


1 Answers

Bitwise NOT, just like in C.

In two's complement representation, ~n is equivalent to -n - 1.

like image 184
dan04 Avatar answered Sep 18 '22 20:09

dan04