I'm setting a value (PID) in the 13 bits allocated as per the mask.. i'm trying to set a flag on bit 4 as well as keeping the original value in the 13 bit mask..
Thoughts??
header = 0x0
pid_mask = 0x1fff
TP_mask = 0x2000
PID = 0x1FFe #8190
TP = 0x1
header = ((header & pid_mask) | (PID << 8))
print(bin(header))
print(hex(header))
print(int(header))
header = ((header & TP_mask) | (TP << 12))
print(bin(header))
print(hex(header))
print(int(header))
And this is my output
This part is good (first print statements)
0b111111111111000000000
0x1ffe00
2096640
This part I expect to be the above + the additional bit flip (second print statements)
0b11000000000000
0x3000
12288
Update
I was being a total Muppet, thanks to @Lesiak for all his help
Here is the updated code
def createHeader(pid,tei,pusi,tranportPriority,tsc,afc,cc):
header = pid << 8
if tei == 1:
header = header | (0x1 << (22 + 1))
print(bin(header))
if pusi == 1:
header = header | (0x1 << (21 + 1))
print(bin(header))
if tranportPriority == 1:
header = header | (0x1 << (8 + 13))
print(bin(header))
header = header | (tsc << 6)
print(bin(header))
header = header | (afc << 4)
print(bin(header))
header = header | (cc << 0)
print(bin(header))
print(hex(header))
return header
header = (createHeader(pid,1,1,1,0x1,0x1,0x2))
The only problem I have is a have to slice off the last byte header[1:4] as it returns 4 bytes not 3. Not sure why....