Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8537869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:00:16+00:00 2026-06-11T11:00:16+00:00

I have such list in Python: [1,0,0,0,0,0,0,0] . Can I convert it to integer

  • 0

I have such list in Python: [1,0,0,0,0,0,0,0]. Can I convert it to integer like as I’ve typed 0b10000000 (i.e. convert to 128)?
I need also to convert sequences like [1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0] to integers (here it will return 0b1100000010000000, i.e. 259).
Length of list is always a multiple of 8, if it is necessary.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:00:17+00:00Added an answer on June 11, 2026 at 11:00 am

    You can use bitshifting:

    out = 0
    for bit in bitlist:
        out = (out << 1) | bit
    

    This easily beats the “int cast” method proposed by A. R. S., or the modified cast with lookup proposed by Steven Rumbalski:

    >>> def intcaststr(bitlist):
    ...     return int("".join(str(i) for i in bitlist), 2)
    ... 
    >>> def intcastlookup(bitlist):
    ...     return int(''.join('01'[i] for i in bitlist), 2)
    ... 
    >>> def shifting(bitlist):
    ...     out = 0
    ...     for bit in bitlist:
    ...         out = (out << 1) | bit
    ...     return out
    ... 
    >>> timeit.timeit('convert([1,0,0,0,0,0,0,0])', 'from __main__ import intcaststr as convert', number=100000)
    0.5659139156341553
    >>> timeit.timeit('convert([1,0,0,0,0,0,0,0])', 'from __main__ import intcastlookup as convert', number=100000)
    0.4642159938812256
    >>> timeit.timeit('convert([1,0,0,0,0,0,0,0])', 'from __main__ import shifting as convert', number=100000)
    0.1406559944152832
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a list in python such as: stuff = [1, 2, 3,
i have such function to solve some logic riddle iloczyny is list with such
I have such a class: public class Cycle { public List<int> Edges { get;
I have such xml: <bean id=conversionService class=org.springframework.context.support.ConversionServiceFactoryBean> <property name=converters> <list> <bean class=converters.AddressToStringConverter /> <bean
Suppose I have an IEnumerable such as a List(TValue) and I want to keep
If I have a method in MyClass such as setSuperClassList(List<Superclass>) ...should I be able
I have a list of lines with attributes such as Brush, line thickness, etc.
I have a list of values as such: row = ['0x14', '0xb6', '0xa1', '0x0',
I have a List of HashMap such as below ArrayList l = new ArrayList
I have a list containing version strings, such as things: versions_list = [1.1.2, 1.0.0,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.