0

Want to extract bits from a binary or integer value. From one end i am sending hex value of 01 00 00 in a byte array.After receiving in other end, converting into integer then try to get range of bits from that. hex(100000) = 65536. Now i want range of bits from this value.


input_2 = 65536
result= bin(input_2)

The problem here is , its 3 byte(24bits) data so it should able give 19-24 bits value.

0001 0000 0000 0000 0000 0000

so i want bit range from 19-24. How can i do?

VVK kumar
  • 267
  • 3
  • 5
  • 15
  • can you explain what you are doing with an example – deadshot Jun 19 '20 at 10:14
  • if you want 19-24 bits why are you using `-24:-19` negative index doesn't work that way -19 doesn't give 19th bit – deadshot Jun 19 '20 at 10:17
  • 1
    Note that the value in your last example line is 1048576, not 65536. It's unclear if that is meant to be that way. – 9769953 Jun 19 '20 at 10:19
  • Modified the explanation please have a look guys. – VVK kumar Jun 19 '20 at 10:24
  • I think this will give you the answer: ```format(65536,'#024b') '0b0000010000000000000000'``` I will not post this as answer since this solution was given by @Martijn in the link https://stackoverflow.com/questions/16926130/convert-to-binary-and-keep-leading-zeros-in-python which @Qiu has commented to be a duplicate. – JenilDave Jun 19 '20 at 10:33
  • What are you really trying to do? Apparently the endianness of the sender and receiver differ. Extracting the 19th to 24th (inclusive) bits is done using bitwise operators, like `(x & 0xfc_00_00) >> 18` – Jan Christoph Terasa Jun 19 '20 at 10:36
  • @JenilDave anyway i got idea to deal with thanks. – VVK kumar Jun 19 '20 at 10:52

0 Answers0