I have a SystemC function with signature:
sc_dt::sc_uint<12> Get()
and the lines:
cerr << "[" << hex << setw(3) << setfill('0') << 0 << dec << "]\n";
cerr << "[" << hex << setw(3) << setfill('0') << Get() << dec << "]\n";
result in this output:
[000]
[0000]
Why does the displayed width change from 3 to 4?
#include <systemc.h>
#include <iostream>
#include <iomanip>
int sc_main(int argc, char* argv[])
{
sc_dt::sc_uint <12> my_uint = 0;
std::cerr << std::hex << my_uint << std::endl;
}
g++ test.cpp -lsystemc && ./a.out
prints this:
SystemC 2.3.1-Accellera --- Jul 24 2017 21:50:41
Copyright (c) 1996-2014 by all Contributors,
ALL RIGHTS RESERVED
0000
It is showing four zeros (for 16 bits) instead of three (for 12 bits), as you probably expected it would, because that is how 12-bit integer is implemented in SystemC. And it doesn't get shortened by std::setw
because it sets the minimum number of characters to be written. If there are more characters, then all of them will get written. Also to mention, the std::dec
in your example does nothing because there are no numbers printed afterwards.
http://www.cplusplus.com/reference/ios/ios_base/width/
http://www.cplusplus.com/reference/iomanip/setw/
This will print only last 3 characters for lower 12 bits:
#include <systemc.h>
#include <iostream>
#include <iomanip>
const unsigned CHARS = 3;
const unsigned MASK = (1u << CHARS * 4) -1; // Same as 0xFFF
int sc_main(int argc, char* argv[])
{
sc_dt::sc_uint <12> my_uint = 0xABC;
std::cerr << std::hex
<< std::setw (CHARS) << std::setfill ('0')
<< (my_uint & MASK) << std::endl;
}