Python-删除按位移位运算符引起的括号,在加法期间会得到不同的结果

我正在使用Karatsuba算法编码乘法,它需要一些移位。简而言之,我需要将此数学表达式转换为编程语句:

Python-删除按位移位运算符引起的括号,在加法期间会得到不同的结果

没有任何括号并确认加法(+)优先于按位移位运算符(,我编写了以下语句:

product = (high_part<<m)<<m + middle_part<<m + low_part

对于high_part,middle_part,low_part = 0,1,这得到product = 0。如果计算得出的正确答案是 2

对于语句中变量的不同顺序,我得到不同的答案。例如,high_part,1

product = middle_part<<m + low_part + (high_part<<m)<<m

给予product = 4

好吧,当我在三个加数之间加上括号时,我得到了正确的答案(product = 2):

product = ((high_part<<m)<<m) + (middle_part<<m) + (low_part)

这是优先事项吗?还是我在这里遗漏了一些奇怪的东西?

lanybo 回答:Python-删除按位移位运算符引起的括号,在加法期间会得到不同的结果

如果仔细查看Python documentation on operator precedence,您会发现<<*之前适用(与+import urllib.request as req from bs4 import BeautifulSoup url = input('enter a full website address: ') html = req.urlopen(url) # request the initial page soup = BeautifulSoup(html,'html.parser') for styles in soup.select('style'): # get in-page style tags print('in page style:') print(styles.string) for link in soup.find_all('link',type='text/css'): # get links to external style sheets address = link['href'] # the address of the stylesheet if address.startswith('/'): # relative link address = url + address css = req.urlopen(address).read() # make a request to download the stylesheet from the address print('linked stylesheet') print(css) 之前适用)相同。 / p>

本文链接:https://www.f2er.com/3087134.html

大家都在问