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, i.e 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