Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #28663

Re: Bitshifts and "And" vs Floor-division and Modular

Date 2012-09-06 21:46 -0400
From Dave Angel <d@davea.name>
Subject Re: Bitshifts and "And" vs Floor-division and Modular
References <d8d77115-dcb2-4769-a592-5fca0fc264bc@googlegroups.com> <df72ea70-4ae8-45e1-9743-64770170b901@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.344.1346982399.27098.python-list@python.org> (permalink)

Show all headers | View raw


On 09/06/2012 09:30 PM, jimbo1qaz wrote:
> On Thursday, September 6, 2012 5:01:12 PM UTC-7, jimbo1qaz wrote:
>> Is it faster to use bitshifts or floor division? And which is better, & or %?
>>
>> All divisors and mods are power of 2, so are binary operations faster? And are they considered bad style?
> OK, I decided to change my code. Which raises a similar question: Which one is better for setting a bit of a byte: |= or +=, assuming each will only be run once? Intuitively, I think |=, but some timeits are inconclusive, mainly because I don't know how it works.

Maybe i should have been clearer in my message.  i don't think you'll
find a meaningful difference unless you're doing longs of a few hundred
digits.  So if the algorithm is to OR on a bit, please use a |=
augmented assignment.  not only will it ward off probable bugs when you
accidentally try to set the bit a second time, but it reads better for
the reader of the program.  The reader is more important than the compiler.

-- 

DaveA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Bitshifts and "And" vs Floor-division and Modular jimbo1qaz <jimmyli1528@gmail.com> - 2012-09-06 17:01 -0700
  Re: Bitshifts and "And" vs Floor-division and Modular Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-09-07 01:30 +0100
    Re: Bitshifts and "And" vs Floor-division and Modular jimbo1qaz <jimmyli1528@gmail.com> - 2012-09-06 18:05 -0700
    Re: Bitshifts and "And" vs Floor-division and Modular jimbo1qaz <jimmyli1528@gmail.com> - 2012-09-06 18:05 -0700
  Re: Bitshifts and "And" vs Floor-division and Modular jimbo1qaz <jimmyli1528@gmail.com> - 2012-09-06 18:30 -0700
    Re: Bitshifts and "And" vs Floor-division and Modular Dave Angel <d@davea.name> - 2012-09-06 21:46 -0400
    Re: Bitshifts and "And" vs Floor-division and Modular Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-07 02:23 +0000
  Re: Bitshifts and "And" vs Floor-division and Modular Dave Angel <d@davea.name> - 2012-09-06 21:36 -0400
  Re: Bitshifts and "And" vs Floor-division and Modular Terry Reedy <tjreedy@udel.edu> - 2012-09-06 21:53 -0400
  Re: Bitshifts and "And" vs Floor-division and Modular Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-07 02:00 +0000
    Re: Bitshifts and "And" vs Floor-division and Modular Grant Edwards <invalid@invalid.invalid> - 2012-09-07 14:19 +0000
  Re: Bitshifts and "And" vs Floor-division and Modular rusi <rustompmody@gmail.com> - 2012-09-06 20:38 -0700
    Re: Bitshifts and "And" vs Floor-division and Modular Paul Rubin <no.email@nospam.invalid> - 2012-09-06 21:32 -0700
      Re: Bitshifts and "And" vs Floor-division and Modular rusi <rustompmody@gmail.com> - 2012-09-07 09:59 -0700
        Re: Bitshifts and "And" vs Floor-division and Modular Dave Angel <d@davea.name> - 2012-09-07 13:12 -0400

csiph-web