how to output an int in binary?

后端 未结 5 1579
醉梦人生
醉梦人生 2020-12-09 17:41
int x = 5;
cout<<(char)x;

the code above outputs an int x in raw binary, but only 1 byte. what I need it to do is output the x as 4-bytes in

5条回答
  •  既然无缘
    2020-12-09 18:33

    A bit late, but, as Katy shows in her blog, this might be an elegant solution:

    #include 
    #include 
    
    int main(){
      int x=5;
      std::cout<(x)<

    taken from: https://katyscode.wordpress.com/2012/05/12/printing-numbers-in-binary-format-in-c/

提交回复
热议问题