Python Forum
Unable to bit shift and logical OR bytes and ints?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unable to bit shift and logical OR bytes and ints?
#1
Python 3.8.2
Windows 10
Powershell

I'm trying to grab a set of 32 bit little-endian pointers from the headers in a set of 256 files, and print them sequentially with line breaks between them.

import sys,glob

def printFileAddr(fileName):
    with open(fileName,"rb") as file:
        file.seek(36,0)
        address = file.read(1)
        address = ((file.read(1) << 8) | address)
        address = ((file.read(1) << 16) | address)
        address = ((file.read(1) << 24) | address)
        print(str(address) + "\n")

for arg in sys.argv[1:]:
    for files in glob.glob(arg):
        printFileAddr(files)
Output
PS C:\Users\pc\Projects\romhack\sagaFrontier\scripts> python .\printArc.py .\M000.ARC         
Traceback (most recent call last):                                                            
  File ".\printArc.py", line 14, in <module>                                                  
    printFileAddr(files)                                                                      
  File ".\printArc.py", line 7, in printFileAddr                                              
    address = ((file.read(1) << 8) | address)                                                 
TypeError: unsupported operand type(s) for <<: 'bytes' and 'int'                              
PS C:\Users\pc\Projects\romhack\sagaFrontier\scripts>                                         
1. Why doesn't Python allow one to bitshift numbers? Why have bitwise operators if you can't use them?

Here's what it prints when I comment out the lines with bit shifting and logical OR.
PS C:\Users\pc\Projects\romhack\sagaFrontier\scripts> python .\printArc.py .\M000.ARC         
b'`'
2. Why is it not printing the numeric value of the byte?
Reply


Messages In This Thread
Unable to bit shift and logical OR bytes and ints? - by MysticLord - Aug-29-2020, 08:02 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Find a shift between 2 pictures TazFleck 0 1,168 Jan-18-2023, 09:56 PM
Last Post: TazFleck
  Help with Logical error processing List of strings dmc8300 3 1,097 Nov-27-2022, 04:10 PM
Last Post: Larz60+
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,531 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  Convert String of an int array to a Numpy array of ints mdsousa 5 5,711 Apr-08-2021, 08:00 PM
Last Post: mdsousa
  Greedy algorithms on logical problems Opensourcehacker 0 1,542 Nov-22-2020, 05:12 PM
Last Post: Opensourcehacker
  List index out of range error when attempting to make a basic shift code djwilson0495 4 3,009 Aug-16-2020, 08:56 PM
Last Post: deanhystad
  Writing a basic shift code djwilson0495 2 2,280 Aug-16-2020, 01:52 PM
Last Post: djwilson0495
  Basic logical errors cpopescu 3 2,069 Jun-03-2020, 11:30 AM
Last Post: menator01
  Python logical operator AND rasec70 4 2,541 May-07-2020, 03:40 PM
Last Post: pyzyx3qwerty
  Pynput doesn't recognize shift button and special characters VirtualDreamer 0 3,039 Jul-17-2019, 11:55 AM
Last Post: VirtualDreamer

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020