【问题标题】:How to add a bit in a short int如何在短整数中添加一点
【发布时间】:2012-10-26 15:22:22
【问题描述】:

我在给定位置放置 1 位时遇到问题。 例如,如果 int e 为 1,我将 neset_t* ne 中的第一位设为 1。

到目前为止我已经这样做了,但没有用。

typedef unsigned short int neset_t;

    void add(neset_t* ne,int e){

    int num = (int) *ne;
    num = num | e;
    *ne = (neset_t) num;
}

提前致谢。 对不起我的英语。

【问题讨论】:

    标签: int bit short


    【解决方案1】:

    e 是位位置吗? 在这种情况下,您应该使用

    数量 = 数量 | (1

    改为

    数量 = 数量 | e;

    【讨论】:

      【解决方案2】:

      如果 e 是你应该做的位位置num |= (1 << e)

      【讨论】:

        猜你喜欢
        • 2023-02-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-26
        • 2015-07-13
        • 2013-02-14
        相关资源
        最近更新 更多