Write 13 bit Value into a binary file

Hi, I am trying to write a 13 bit unint value to a binary file using .write but the value seems to be wrong.

uint8 u8;
u32 = (u8<<5) + (u8>>3); //Logic to convert to u13
_file.open(_filename.c_str(), std::ofstream::app);
_file.write((char *)&u32, sizeof(u32));

What is the correct approach ?

In reply to priyansh.ag:

This looks like C++ code?? What libraries are you using for these types?
Binary file writing is in groups of 8 bits.