'~' 是什么意思?在 python 中是什么意思?

发布于 2024-09-05 13:06:46 字数 907 浏览 3 评论 0原文

python 中的“~”是什么意思?

我不久前在 python 中发现了这个 BF 解释器。

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

我想知道它的作用,因为我想在我的 ti 84 上制作一个(和一个 PF )

BF 是 http://en.wikipedia.org/wiki/Brainfuck 和 PF 是类似的东西

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北方。的韩爷 2024-09-12 13:06:46

按位 NOT,就像 C 中一样。

在二进制补码表示中,~n 相当于 -n - 1

Bitwise NOT, just like in C.

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

南风起 2024-09-12 13:06:46

在这种特定情况下,只需将“~”替换为“not”即可。

附言。好吧,我想我必须解释一下 - 开始被 -1 打脸,可能的前提是我不知道逻辑和按位否定之间的区别。

问题是,问题中的代码已损坏。其中有一个错误。如果您检查 Brainfuck 应该如何工作,它会在当前内存单元为 !=0 时在 [ ] 大括号内循环(这是在输入 [ 时作为先决条件进行检查的,并且作为从]返回之前进行优化)。

但与其争论,也许用代码不工作的例子来展示更容易。让我们看一下简单的程序'[+]'。尝试调整它应该退出(因为当前单元格为 0,它甚至不会进入循环)。相反,如果您在此解释器中运行它,它将进入无限循环。

因此,如果我的澄清现在有意义,我会恳请您恢复您的 -1 票;-)

这是稍微美化的解释器,修复了 ~ 错误,我还添加了缺少的 输入:

from sys import stdin, stdout

bfHelloWorld = '++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.'

# http://esoteric.sange.fi/brainfuck/bf-source/prog/yapi.b
bfPiDigits = '''>  +++++ (5 digits)
[<+>>>>>>>>++++++++++<<<<<<<-]>+++++[<+++++++++>-]+>>>>>>+[<<+++[>>[-<]<[>]<-]>>
[>+>]<[<]>]>[[->>>>+<<<<]>>>+++>-]<[<<<<]<<<<<<<<+[->>>>>>>>>>>>[<+[->>>>+<<<<]>
>>>>]<<<<[>>>>>[<<<<+>>>>-]<<<<<-[<<++++++++++>>-]>>>[<<[<+<<+>>>-]<[>+<-]<++<<+
>>>>>>-]<<[-]<<-<[->>+<-[>>>]>[[<+>-]>+>>]<<<<<]>[-]>+<<<-[>>+<<-]<]<<<<+>>>>>>>
>[-]>[<<<+>>>-]<<++++++++++<[->>+<-[>>>]>[[<+>-]>+>>]<<<<<]>[-]>+>[<<+<+>>>-]<<<
<+<+>>[-[-[-[-[-[-[-[-[-<->[-<+<->>]]]]]]]]]]<[+++++[<<<++++++++<++++++++>>>>-]<
<<<+<->>>>[>+<<<+++++++++<->>>-]<<<<<[>>+<<-]+<[->-<]>[>>.<<<<[+.[-]]>>-]>[>>.<<
-]>[-]>[-]>>>[>>[<<<<<<<<+>>>>>>>>-]<<-]]>>[-]<<<[-]<<<<<<<<]++++++++++.
'''

code = bfPiDigits   # the code
data = [0] * 255    # data memory
cp = 0              # code pointer
dp = 0              # data pointer

while cp < len(code):
    cmd = code[cp]
    if   cmd == '>': dp += 1
    elif cmd == '<': dp -= 1
    elif cmd == '+': data[dp] += 1 
    elif cmd == '-': data[dp] -= 1 
    elif cmd == '.': stdout.write(chr(data[dp]))
    elif cmd == ',': data[dp] = ord(stdin.read(1))
    elif cmd == '[' and not data[dp]: # skip loop if ==0
        n = 0
        while True:
            cmd = code[cp]
            if   cmd == '[': n += 1
            elif cmd == ']': n -= 1
            if not n: break
            cp += 1
    elif cmd == ']' and data[dp]:  # loop back if !=0
        n = 0
        while True:
            cmd = code[cp]
            if   cmd == '[': n+=1
            elif cmd == ']': n-=1
            if not n: break
            cp -= 1
    cp += 1

In this particular context, just replace '~' with 'not'.

PS. ok i guess i will have to explain - started getting slapped with -1's, probably on the premise i don't know the difference between logical and bitwise negation.

The thing is, the code in the question is broken. There is a bug in it. If you check how Brainfuck should work, it loops within [ ] braces while the current memory cell is !=0 (this is checked as pre-condition when entering [ and as optimization before returning from ]).

But instead of arguing, perhaps is easier to show with examples of the code not working. Let's take the simple program '[+]'. Trying to tun this should just exit (because current cell is 0, it won even enter the loop). Instead if you run it in this interpreter, it goes into infinite loop.

So i'll kindly ask you to revert your -1 votes if my clarification makes sense now ;-)

Here is the interpreter slightly beautified, with fixed ~ bug and i also added the missing , input:

from sys import stdin, stdout

bfHelloWorld = '++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.'

# http://esoteric.sange.fi/brainfuck/bf-source/prog/yapi.b
bfPiDigits = '''>  +++++ (5 digits)
[<+>>>>>>>>++++++++++<<<<<<<-]>+++++[<+++++++++>-]+>>>>>>+[<<+++[>>[-<]<[>]<-]>>
[>+>]<[<]>]>[[->>>>+<<<<]>>>+++>-]<[<<<<]<<<<<<<<+[->>>>>>>>>>>>[<+[->>>>+<<<<]>
>>>>]<<<<[>>>>>[<<<<+>>>>-]<<<<<-[<<++++++++++>>-]>>>[<<[<+<<+>>>-]<[>+<-]<++<<+
>>>>>>-]<<[-]<<-<[->>+<-[>>>]>[[<+>-]>+>>]<<<<<]>[-]>+<<<-[>>+<<-]<]<<<<+>>>>>>>
>[-]>[<<<+>>>-]<<++++++++++<[->>+<-[>>>]>[[<+>-]>+>>]<<<<<]>[-]>+>[<<+<+>>>-]<<<
<+<+>>[-[-[-[-[-[-[-[-[-<->[-<+<->>]]]]]]]]]]<[+++++[<<<++++++++<++++++++>>>>-]<
<<<+<->>>>[>+<<<+++++++++<->>>-]<<<<<[>>+<<-]+<[->-<]>[>>.<<<<[+.[-]]>>-]>[>>.<<
-]>[-]>[-]>>>[>>[<<<<<<<<+>>>>>>>>-]<<-]]>>[-]<<<[-]<<<<<<<<]++++++++++.
'''

code = bfPiDigits   # the code
data = [0] * 255    # data memory
cp = 0              # code pointer
dp = 0              # data pointer

while cp < len(code):
    cmd = code[cp]
    if   cmd == '>': dp += 1
    elif cmd == '<': dp -= 1
    elif cmd == '+': data[dp] += 1 
    elif cmd == '-': data[dp] -= 1 
    elif cmd == '.': stdout.write(chr(data[dp]))
    elif cmd == ',': data[dp] = ord(stdin.read(1))
    elif cmd == '[' and not data[dp]: # skip loop if ==0
        n = 0
        while True:
            cmd = code[cp]
            if   cmd == '[': n += 1
            elif cmd == ']': n -= 1
            if not n: break
            cp += 1
    elif cmd == ']' and data[dp]:  # loop back if !=0
        n = 0
        while True:
            cmd = code[cp]
            if   cmd == '[': n+=1
            elif cmd == ']': n-=1
            if not n: break
            cp -= 1
    cp += 1
空城缀染半城烟沙 2024-09-12 13:06:46

并提出一件事,其他答案都没有提到:用户定义类的 ~ 行为可以通过覆盖 __invert__ 方法(或 nb_invert 槽(如果您使用的是 Python/C API)。

And to bring up one thing none of the other answers mentioned: the behavior of ~ for user-defined classes can be changed by overriding the __invert__ method (or the nb_invert slot if you're using the Python/C API).

巴黎盛开的樱花 2024-09-12 13:06:46

~ 是按位非。

我真的想不出一个好的方法来说明它(除非你知道 -10 的按位否定),但是 维基百科条目非常好。

~ is bitwise-not.

I can't really think of a good way to illustrate it (unless you know that -1 is the bitwise negation of 0), but the wikipedia entry is pretty good.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文