Issue
I 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.
Solution
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
.
Answered By - Tim Roberts
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.