简体   繁体   中英

Bitwise Operations on a 16 bit number

I am having trouble figuring out how to create a 16 bit int and set/maniuplate all the individual bits. What would the code be if I want my int to start out with all 16 bits = 0?

If I declare my int as

int16_t bitNum = 0; 

Is this the same as 0000000000000000? And how do I access the values of the individual bits? Thanks for your time.

Is this the same as 0000000000000000?

Yes.

And how do I access the values of the individual bits?

You cannot access real individual bit as the smaller variable computer can address and allocate is a char (a char variable is of the natural size to hold a character on a given machine). But you can manipulate each bit using bit masks ( and bitwise operations)

temp & (1 << N) // this will test N-th bit

or in C++ you can use std::bitset to represent a sequence of bits.

#include <bitset>
#include <iostream>
#include <stdint.h>

int main()
{
    uint16_t temp = 0x0;
    std::bitset< 16>   bits( temp);

    // 0 -> bit 1
    // 2 -> bit 3
    std::cout << bits[2] << std::endl;
}

This is what Bjarne Stroustrup says about operations on bits in "C++ Prog... 3d edition" 17.5.3 Bitset:

C++ supports the notion of small sets of flags efficiently through bitwise operations on integers (§6.2.4). These operations include & (and), | (or), ^ (exclusive or), << (shift left), and >> (shift right).

Well, also ~ , bitwise complement operator, the tilde, that flips every bit.

Class bitset generalizes this notion and offers greater convenience by providing operations on a set of N bits indexed from 0 through N-1, where N is known at compile time. For sets of bits that don't fit into long int using a bitset is much more convenient than using integers directly. For smaller sets, there may be an efficiency tradeoff. If you want to name the bits, rather than numbering them, using a set (§17.4.3), an enumeration (§4.8), or a bitfield (§C.8.1) are alternatives. (...) A key idea in the design of bitset is that an optimized implementation can be provided for bitsets that fit in a single word. The interface reflects this assumption.

So there are alternatives, ie another option is to use a bitfields . They are binary variables bundled together as fields in a struct. You can then access each individual "bit" using access operator: . for references or -> for pointers.

struct BitPack {
    bool b1 :  0;
    bool b2 :  0;
    //...
    bool b15 : 0;
};

void f( BitPack& b)
{
  if( b.b1) // if b1 is set
      g();
}

links:

http://en.cppreference.com/w/cpp/utility/bitset

http://en.cppreference.com/w/cpp/language/bit_field

Setting an object of an integral type to zero means setting all its used bits to zero.

You could write two functions. one will set a specified bit (starting from 0) and other will reset a specified bit. For example

#include <iostream>
#include <cstdint>

inline uint16_t & set( uint16_t &bitNum, size_t n )
{
    return ( bitNum |= 1 << n );
}

inline uint16_t & reset( uint16_t &bitNum, size_t n )
{
    return ( bitNum &= ~( 1 << n ) );
}

int main()
{
    uint16_t bitNum = 0;

    for ( size_t i = 0; i < 16; i++ )
    {
        std::cout << set( bitNum, i ) << std::endl;
        reset( bitNum, i );
    }

    return 0;
} 

The output is

1
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768

The other way is to use standard class std::bitset declared in header <bitset> It already has the corresponding functions.

For example

#include <iostream>
#include <bitset>

int main()
{
    std::bitset<16> bitNum;

    for ( size_t i = 0; i < 16; i++ )
    {
        std::cout << bitNum.set( i ) << std::endl;
        bitNum.reset( i );
    }

    return 0;
} 

The output is

0000000000000001
0000000000000010
0000000000000100
0000000000001000
0000000000010000
0000000000100000
0000000001000000
0000000010000000
0000000100000000
0000001000000000
0000010000000000
0000100000000000
0001000000000000
0010000000000000
0100000000000000
1000000000000000

Enjoy!:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM