changing a single bit of an unsigned interger variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 3bottles
    New Member
    • Aug 2007
    • 2

    changing a single bit of an unsigned interger variable

    Hi,
    What i'm trying to write a code that would allow me change a single bit of a variable. So I've tried to store it in an array so that its possible for me to change a single bit of the variable by simply changing the value of the array index.
    However this doesn't seem to work. See extract of code below.

    Help please...ideas, suggestions?

    Code:
    typedef u_int32_t UInt32;
    UInt32 value = 0xFFFFFFFF;
    int index = 4;
    
    UInt32* var = new UInt32[32];
    *var = value;
    var[index] = 0;
    
    cout<<hex<<*chan<<endl;
  • scruggsy
    New Member
    • Mar 2007
    • 147

    #2
    Originally posted by 3bottles
    Hi,
    What i'm trying to write a code that would allow me change a single bit of a variable. So I've tried to store it in an array so that its possible for me to change a single bit of the variable by simply changing the value of the array index.
    However this doesn't seem to work. See extract of code below.

    Help please...ideas, suggestions?

    Code:
    typedef u_int32_t UInt32;
    UInt32 value = 0xFFFFFFFF;
    int index = 4;
    
    UInt32* var = new UInt32[32];
    *var = value;
    var[index] = 0;
    
    cout<<hex<<*chan<<endl;
    That's not going to work.
    To manipulate individual bits you can use the bit-wise operators or a bitset. You should probably learn how the bit-wise operators work since that is rather fundamental to an understanding of how computer store and manipulate data.
    Examples in binary:
    0001 | 0010 = 0011
    0111 & 1001 = 0001
    ~0101 = 1010

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Originally posted by scruggsy
      You should probably learn how the bit-wise operators work
      Actually, for C++ you should learn the bitset<> template. The bitwise operators are very low level and are rarely used in C++ unless the C++ is being written by a C programmer.

      Comment

      • 3bottles
        New Member
        • Aug 2007
        • 2

        #4
        Originally posted by scruggsy
        That's not going to work.
        To manipulate individual bits you can use the bit-wise operators or a bitset. You should probably learn how the bit-wise operators work since that is rather fundamental to an understanding of how computer store and manipulate data.
        Examples in binary:
        0001 | 0010 = 0011
        0111 & 1001 = 0001
        ~0101 = 1010
        Thanks. But my reluctance to masking is that there are 32 possible values for index. So that would mean writing 32 possible conditions. Isn't there a shorter way of doing this?

        Comment

        • scruggsy
          New Member
          • Mar 2007
          • 147

          #5
          Originally posted by 3bottles
          Thanks. But my reluctance to masking is that there are 32 possible values for index. So that would mean writing 32 possible conditions. Isn't there a shorter way of doing this?
          Yes, use the left shift operator.
          More examples in binary:
          1 << 0 = 1
          1 << 1 = 10
          1 << 2 = 100 etc...

          This means all you need to know is the position of the bit (0 thru 31) you want to change. Use the shift operator to get your mask, then & or | to flip your bit.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Again, I say: Use a bitset.

            [code=cpp]
            bitset<32> thebits;
            thebits.set(19) ; //bit 19 is 1
            thebits.reset(1 9); //bit 19 is 0
            thebits.test(19 ); //true if bit 19 is 1
            //etc
            [/code]

            You can use an enum to name your bits:
            [code=cpp]
            enum bits {POWER_ON, PAPER_LOADED, TONER_LOW};
            [/code]

            then use the enum names instead of the bit number.

            Those bitwise operators are supposed to remain in C.

            Comment

            Working...