'shifting result using << [closed]

enter image description hereI want to calculate the string value that is return from a function, which is a hex value in string type. I used int(,16) to convert it to int, but I still cannot use the shift operator, any reason why?

       reg_dic= { "REG_RX_PKT_CNT_L_ADDR":"00000", 
               "REG_RX_PKT_CNT_H_ADDR":"00001", 
               "REG_RX_PKT_CNT_B_L_ADDR":"0001F", 
               "REG_RX_PKT_CNT_B_H_ADDR":"00020"
                 }
result_l, value_low = self.check_regs(portnum, list(reg_dic.values())[pair])
result_h, value_high = self.check_regs(portnum,list(reg_dic.values())[pair+1])
value_low = int(value_low, 16)
value_high = int(value_high, 16)
print(type(value_low))
value = value_high << 18 + value_low
print(list(reg_dic.keys())[pair], value_low)
print(list(reg_dic.keys())[pair+1], value_high)
print("Counter Sum is:",  hex(value)) 

Result I got is bunch of 0 and I have to break it.

Thanks in adv enter image description here



Solution 1:[1]

Your NEW problem is one of precedence. + has higher precedence than << so your statement is parsed like value = value_high << (18 + value_low).

To solve this, add parentheses explicitly, like: value = (value_high << 18) + value_low.

Solution 2:[2]

value = value_high << 18 + value_low should be value = value_hight << 18 + value_low value_high is still a string. value_hight is the one you casted to int

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Karl Knechtel
Solution 2 TheQAGuy