Python Forum
Convert list of integers to bit-mapped byte
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert list of integers to bit-mapped byte
#5
Hello!
Here is how to set the n-th bit to 1
def set_bit(num, bit_pos):
    if bit_pos == 1:
        return num | 1
    else:
        return num | ( 1<<(bit_pos - 1))

selenoids = 0
for n in [1,3,4,7]:
    selenoids = set_bit(selenoids, n)
To unset the n-th bit just copy the same function, change the name to unset_bit and replace '|' with '&'. The first one is bitwise OR and the second is AND.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Messages In This Thread
RE: Convert list of integers to bit-mapped byte - by wavic - Oct-24-2017, 11:16 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,372 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Timestamp of file changes if a share mapped to a server…. tester_V 34 4,376 Jul-04-2023, 05:19 AM
Last Post: tester_V
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,735 Mar-24-2023, 08:34 AM
Last Post: fullytotal
Question How to append integers from file to list? Milan 8 1,606 Mar-11-2023, 10:59 PM
Last Post: DeaD_EyE
  convert string to float in list jacklee26 6 2,051 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  Error "list indices must be integers or slices, not str" dee 2 1,602 Dec-30-2022, 05:38 PM
Last Post: dee
  convert a list to links Pir8Radio 3 1,174 Nov-28-2022, 01:52 PM
Last Post: Pir8Radio
  convert this List Comprehensions to loop jacklee26 8 1,649 Oct-21-2022, 04:25 PM
Last Post: deanhystad
  read a text file, find all integers, append to list oldtrafford 12 3,930 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  Convert list to interger Clives 5 1,718 May-09-2022, 12:53 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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