Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!xlned.com!feeder7.xlned.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'binary': 0.07; 'error:': 0.07; 'explicit': 0.07; 'string': 0.09; '101': 0.09; 'collier': 0.09; 'function,': 0.09; 'interpreted': 0.09; 'latter': 0.09; 'way:': 0.09; 'python': 0.11; 'bitwise': 0.16; 'command.': 0.16; 'consciously': 0.16; 'digits.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'literal,': 0.16; 'literals.': 0.16; 'non-string': 0.16; 'octal': 0.16; 'operators,': 0.16; 'typeerror:': 0.16; 'values:': 0.16; 'wrote:': 0.18; 'typing': 0.19; '>>>': 0.22; 'errors.': 0.24; 'integer': 0.24; 'helpful': 0.24; 'script': 0.25; 'task': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'google,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'gives': 0.31; 'easier': 0.31; '"",': 0.31; 'decimal': 0.31; 'fixing': 0.31; 'file': 0.32; '(most': 0.33; 'problem': 0.35; "can't": 0.35; 'convert': 0.35; 'johnson': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'doing': 0.36; 'shows': 0.36; 'turn': 0.37; 'being': 0.38; 'to:addr:python-list': 0.38; 'recent': 0.39; 'quote': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'how': 0.40; 'more': 0.64; '30,': 0.65; 'here': 0.66; 'jul': 0.74; 'obvious': 0.74; 'saw': 0.77; '010': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=1ZVDWjasb9HO09lq036mgOtSHfeEqmuFN7S8pEQFuOQ=; b=x8kaSUL3+HnT62xsm4/O0v6i71Dqavl1W3oABETclnGoTOerf1Yqb/IfQarJL1NbWA H0xmd+Nc1nOBVaYuCnPJcr6wjLwYKZHvqA25htXIM5cZszMWNggkv6fZJimaihW5Ub8W F/uht19WUKkvh1YPL/VytK2CgJD7+iWYFooG3j737Ws9SXDki9YO1vy1P+ylo+25bCoN i2OVKvdo/MKanjf7jHGd+y7twWc9PHCq5xRbYtlu9vleRr/EMejGowcB1JprR/5pOwR+ 6z1W1Mf3WfaTcnP9pDOVuKLdBlpjQRrTGpoGxtYO994ecxV7lAFQx+GBvFGnJAoIZnAf PGZw== MIME-Version: 1.0 X-Received: by 10.58.223.238 with SMTP id qx14mr25986664vec.98.1375141459614; Mon, 29 Jul 2013 16:44:19 -0700 (PDT) In-Reply-To: <51F6FBE8.8000106@Gmail.com> References: <51F6FBE8.8000106@Gmail.com> Date: Tue, 30 Jul 2013 00:44:19 +0100 Subject: Re: Bitwise Operations From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1375141462 news.xs4all.nl 15940 [2001:888:2000:d::a6]:42174 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:51524 On Tue, Jul 30, 2013 at 12:34 AM, Devyn Collier Johnson wrote: > > I understand the symbols. I want to know how to perform the task in a script > or terminal. I have searched Google, but I never saw a command. Typing "101 > & 010" or "x = (int(101, 2) & int(010, 2))" only gives errors. Your problem here isn't in the bitwise operators, but in your binary literals. Python deliberately and consciously rejects 010 as a literal, because it might be interpreted either as decimal 10, or as octal (decimal 8), the latter being C's interpretation. Fixing that shows up a more helpful error: >>> x = (int(101, 2) & int(10, 2)) Traceback (most recent call last): File "", line 1, in x = (int(101, 2) & int(10, 2)) TypeError: int() can't convert non-string with explicit base The int() call isn't doing what you think it is, because 101 is already an int. The obvious solution now is to quote the values: >>> x = (int("101", 2) & int("010", 2)) >>> x 0 But there's an easier way: >>> x = 0b101 & 0b010 >>> x 0 I think that might do what you want. Also check out the bin() function, which will turn an integer into a string of digits. ChrisA