The tophat and bottomhat operations are the difference of the source image and the opening/closing of said image with the selected strel. They represent the changes made -- i.e. the objects removed by opening or the holes filled by closing.
If you're trying to write your own, then:
For binary data, they should be equivalent to
outpict = inpict & ~imopen(inpict,se);
outpict = ~inpict & imclose(inpict,se);
For numeric data, they are
outpict = inpict - imopen(inpict,se);
outpict = imclose(inpict,se) - inpict;
The same should apply for 1D cases. Ultimately, opening and closing operations can be further reduced to a set of routines based on a simple moving-window maximum filter.