PYTHON equivalents of BITAND and BITSHIFT of MATLAB

Thomas Jollans tjol at tjol.eu
Thu May 2 02:18:39 EDT 2019


On 02/05/2019 06:47, blmadhavan at gmail.com wrote:
> Hello Brian,
> 
> Thanks for your suggestion. Which is correct for MATLAB command: typeBits = FCF << -9?
> 
> typeBits = FCF >> 9 
> 
> or 
> 
> typeBits = FCF >> -9
> 
> I mean to ask if it should be -9 or +9?

Why don't you just, you know, try them out?

Fire up MATLAB or GNU Octave (here I'm using MATLAB R2016a)

>> bitshift(1024, -9)

ans =

     2

>> bitshift(1024, +9)

ans =

      524288


And fire up Python


Python 3.7.1 | packaged by conda-forge | (default, Nov 13 2018, 18:33:04)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 1024 >> 9
2
>>> 1024 << 9
524288
>>> 1024 << -9
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: negative shift count
>>>



> 
> Thanks in advance
> Madhavan
> 
> On Wednesday, May 1, 2019 at 11:22:10 PM UTC+5:30, Brian Oney wrote:
>> On Wed, 2019-05-01 at 10:35 -0700, blmadhavan at gmail.com wrote:
>>> Hi,
>>>
>>> I have the following line from a MATLAB program with FCF (format: UInt_16) as input:
>>>
>>> ftype = bitand(FCF, 7)
>>> typeBits = bitshift(FCF, -9)
>>> subtype = bitand(typeBits, 7)
>>>
>>> I wrote the following in Python for the above commands:
>>>
>>> ftype = FCF & 7
>>> typeBits = FCF << -9             ------> Is this correct or FCF >> -9?
>>> subtype = typeBits & 7
>>>  
>>> Can someone help me write the equivalent command in PYTHON?
>>>
>>> Look forward to your suggestions.
>>
>> >From the Matlab doc:
>> '
>> intout = bitshift(A,k)
>> intout = bitshift(A,k,assumedtype)
>> Description
>>
>> example
>>
>> intout = bitshift(A,k) returns A shifted to the left by k bits,
>> equivalent to multiplying by 2k. Negative values of k correspond to
>> shifting bits right or dividing by 2|k| and rounding to the nearest
>> integer towards negative infinity. Any overflow bits are truncated. '
>>
>> So the equivalent would be:
>>
>>>>> typeBits = FCF >> 9
>>
>> Cheers
>> Brian
> 




More information about the Python-list mailing list