Python 基礎(chǔ)教程
題目:學(xué)習(xí)使用按位取反~。
程序分析:~0=1; ~1=0;
(1)先使a右移4位。
(2)設(shè)置一個(gè)低4位全為1,其余全為0的數(shù)??捎脋(~0<<4)
(3)將上面二者進(jìn)行&運(yùn)算。
程序源代碼:
#!/usr/bin/python # -*- coding: UTF-8 -*- if __name__ == '__main__': a = 234 b = ~a print 'The a's 1 complement is %d' % b a = ~a print 'The a's 2 complement is %d' % a
以上實(shí)例輸出結(jié)果為:
The a's 1 complement is -235 The a's 2 complement is -235其他擴(kuò)展