Q:

Writing File in binary & Hex with bit manipulation

According to my criteria i need to store data in a long HEX-line.
The problem comes when certain bits of a byte need to be a certain value
EXAMPLE
      Field Name | Size
          A      |  2-Bytes
          B      |  4-Bytes
          C      |  1-Bytes

& further within  Field A of 2 bytes
the 1st 2 bits must represent "type"(either 01 or 10)
& the next 14 bits must represent an integer(Location of type)
      #--------#-----------------------#
      |  TYPE  |       Location        |
      #--------#-----------------------#
        2-bits          14-bits
      ***********************************> Total 2 bytes

Similar process for other fields
How do i store data in a fixed field size
with the required starting bits representing the type
If possible please provide me with a simple example
of, let's say: 
        Type = 01
      & Location = 412

Stored in hex format of size 2 bytes
This is in a C++ project that uses Qt, so you can use Qt APIs.

A:

The answer to your question has nothing to do with Qt, c++ or any other language. All languages will handle this in pretty much the same way.
If you're concerned with specific bits in a variable, you need to use the bitwise operators. 
Your specific example shows that your variable is 2 bytes (16 bits) so you're most likely using an unsigned short int. In Qt, this has been typedefed as quint16
To get the bits for the Type and Location data, you would apply the bitwise operators like so:
quint16 data;
...
Type type = static_cast<Type>((data & 0xC000) >> 14); // assuming Type is an enum
int location = data & 0x3FFF;

To store the bits in a single integer value, you would do the reverse the process:
Type type;
int location;
...
quint16 data = static_cast<int>(type) & 0xFF << 14;
data += location & 0x3FFF;

