首页 \ 问答 \ Python二进制操作 - 组合高位和低位(Python binary operations - combine higher and lower order bits)

Python二进制操作 - 组合高位和低位(Python binary operations - combine higher and lower order bits)

我有两个十六进制值(作为字符串提供):

00000000fe000000 < - 高位32位

000000000001009f < - 低位32位

他们需要合并成一个单一的值,理想情况下 - 像值[0:15](返回0到15,从右到左)方便地处理。

在Python中做这件事的最好方法是什么?


I have two hex values (provided as strings):

00000000fe000000 <- high order 32-bits

000000000001009f <- low order 32-bits

They need to be combined into a single value, and ideally - conveniently addressed like value[0:15] (returning bits 0 to 15, right to left).

What is the best way to do this in Python?


原文:https://stackoverflow.com/questions/50451184
更新时间:2023-09-09 16:09

最满意答案

以下是我要做的事情的草图:

// Beware, brain-compiled code ahead
template< typename T >
class typed_container
{
  typedef std::vector<T> data_t;
public:
  typedef data_t::iterator iterator;
  iterator begin() {return data_.begin();}
  iterator end  () {return data_.end();}
private:
  data_t data_;
};

typedef my_type_list<A,B,C> types_t;

class master : public derive_from< typed_container, types_t > {
  template< typename T >
  struct traits {
    typedef typename typed_container<T>::iterator iterator;
    iterator begin(typed_container<T>& c) {return c.begin();}
    iterator end  (typed_container<T>& c) {return c.end  ();}
  };
public:
  template< typename T > 
  typename traits<T>::iterator begin() {return traits<T>::begin(*this);}
  template< typename T > 
  typename traits<T>::iterator end  () {return traits<T>::end  (*this);}

  typedef my_assembling_iterator<types_t> iterator;

  iterator begin() {return my_assembling_iterator<types_t>.begin(*this);}
  iterator end  () {return my_assembling_iterator<types_t>.end  (*this);}
};

这让你实现my_type_list (相当简单), derive_from (不是那么简单,但也不是太难), my_assembling_iterator (我还没有需要做这样的事情)。


你可以在这里找到一个可用的C ++ 03类型列表实现。 它最多只需要9个模板参数(但这很容易扩展),而且你必须写

typedef my_type_list<A,B,C>::result_t types_t

但它很简单,免费,我知道它的工作原理(因为我自己在使用这个库)。

derive_from模板看起来像这样:

//Beware, brain-compiled code ahead!
template< template<typename> class C, class  >
struct derive_from;

template< template<typename> class C >
struct derive_from< C, nil > {};

template< template<typename> class C, typename Head, typename Tail >
struct derive_from< C, my_type_list<Head,Tail> > : public C<Head>
                                                 , public derive_from<C,Tail> {};

这留下了迭代器。 你有什么需要呢? 它必须是一个随机访问迭代器(硬)还是一个前向迭代器就足够了? 你是否需要任何特定的顺序来遍历元素?


Here's a sketch of what I would do:

// Beware, brain-compiled code ahead
template< typename T >
class typed_container
{
  typedef std::vector<T> data_t;
public:
  typedef data_t::iterator iterator;
  iterator begin() {return data_.begin();}
  iterator end  () {return data_.end();}
private:
  data_t data_;
};

typedef my_type_list<A,B,C> types_t;

class master : public derive_from< typed_container, types_t > {
  template< typename T >
  struct traits {
    typedef typename typed_container<T>::iterator iterator;
    iterator begin(typed_container<T>& c) {return c.begin();}
    iterator end  (typed_container<T>& c) {return c.end  ();}
  };
public:
  template< typename T > 
  typename traits<T>::iterator begin() {return traits<T>::begin(*this);}
  template< typename T > 
  typename traits<T>::iterator end  () {return traits<T>::end  (*this);}

  typedef my_assembling_iterator<types_t> iterator;

  iterator begin() {return my_assembling_iterator<types_t>.begin(*this);}
  iterator end  () {return my_assembling_iterator<types_t>.end  (*this);}
};

That leaves you to implement my_type_list (rather simple), derive_from (not as simple, but not too hard either), and my_assembling_iterator (I hadn't had a need to do something like that yet).


You can find a working C++03 type list implementation here. It only takes up to nine template arguments (but that's easily extended), and you'll have to write

typedef my_type_list<A,B,C>::result_t types_t

but it's simple and free and I know it works (because I'm using this library myself).

The derive_from template would look something like this:

//Beware, brain-compiled code ahead!
template< template<typename> class C, class  >
struct derive_from;

template< template<typename> class C >
struct derive_from< C, nil > {};

template< template<typename> class C, typename Head, typename Tail >
struct derive_from< C, my_type_list<Head,Tail> > : public C<Head>
                                                 , public derive_from<C,Tail> {};

That leaves the iterator. What are your needs regarding it? Does it have to be a random-access iterator (hard) or would a forward iterator suffice? Do you need any particular order to iterate over the elements?

相关问答

更多
  • 在我看来,容器的价值(如容器理论)就是它们的一致性 。 这种一致性使得使用容器表示作为可执行规范的基础,甚至可能使用机器辅助的程序派生。 容器:理论工具,而不是一个很好的运行时数据表示策略 我不会推荐(标准化)容器的固定点作为实现递归数据结构的一种很好的通用方法。 也就是说,知道给定的函子具有(最多为iso)作为容器的演示文件是有帮助的,因为它告诉您可以实例化通用容器功能(这很容易实现,一次完成,归功于均匀性)可以实例化到你特定的仿函数,以及你应该期望的行为。 但这并不是说容器实现将以任何实际的方式高效。 ...
  • 在Delphi XE中,没有理由不使用通用容器。 从旧方法转换到铸造将会给你: 更清洁,类型安全,不易出错的代码, 枚举器,在循环中, 同样 更好的性能特点。 In Delphi XE, there is no reason not to use generic containers. Switching from the old method with casting will give you: cleaner, type-safe, less error-prone code, enumerators ...
  • 风险是通过指向基类 ( delete , delete []和潜在的其他释放方法的指针来解除分配)。 由于这些类( deque , map , string等)没有虚拟的dtors,所以不可能只使用指向这些类的指针来正确清理它们: struct BadExample : vector {}; int main() { vector* p = new BadExample(); delete p; // this is Undefined Behavior return 0; ...
  • 为什么拼写错误名称出现在mysql容器的names列中 因为它们是连在一起的 为什么错字集装箱出口? 您可以通过运行docker logs typo找到线索。 一个常见的错误是让容器在后台而不是在前台运行进程。 Why does the typo name show up in the names column of the mysql container because they are linked together why does the typo container exit? You might ...
  • 我非常喜欢这个解决方案: 在docker容器中运行docker? 这基本上允许您使用在主机上运行的docker。 但如果你真的想在dind ,这里是使用dind图像的官方解决方案: https : dind I quite like this solution: Run docker inside a docker container? which basically allows you to use docker that's running on the host. But if you really ...
  • 以下是我要做的事情的草图: // Beware, brain-compiled code ahead template< typename T > class typed_container { typedef std::vector data_t; public: typedef data_t::iterator iterator; iterator begin() {return data_.begin();} iterator end () {return data_.end( ...
  • 在container_of()的内核定义中使用typeof仅用于编译时类型检查 - 它确保传递的ptr实际上是指向与member相同类型的指针。 它可以修改为完全ANSI C,但需要花费这种类型检查: #define container_of(ptr, type, member) ((type *)((char *)ptr - offsetof(type, member))) ( offsetof()在 ) The use of typeof in the kernel definit ...
  • 这是您需要应用的经典复合设计模式: http://en.wikipedia.org/wiki/Composite_pattern This is the classical Composite design pattern you need to apply: http://en.wikipedia.org/wiki/Composite_pattern
  • 我宁愿说不,因为只有两个非常小的用途: 第一个是IncompleteCholesky ,其中std::vector和std::list用于在计算期间保存一些临时对象,而不是成员。 仅当用户明确使用它时才使用此类。 第二个是SuperLUSupport模块,它是一个支持第三个库的模块。 再说一遍,你不能意外使用! Avi提到的StlSupport模块只是一个辅助模块,可以简化STL容器中Eigen矩阵的存储。 I would rather say no as there are only two very m ...
  • 我能够通过添加自定义TypedFactoryComponentSelector和我的工厂来解决这个问题。 这是实施: public class FormFactoryComponentSelector : DefaultTypedFactoryComponentSelector { protected override Type GetComponentType(MethodInfo method, object[] arguments) { if (arguments.Le ...

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)