Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct R{
- unsigned int opcode: 6;
- unsigned int rs: 5;
- unsigned int rt: 5;
- unsigned int rd: 5;
- unsigned int shamt: 5;
- unsigned int funct: 6;
- };
- struct R test = {32,0,11,21,19,0}
- FILE *fp = fopen("./result", "rb");
- fwrite(&test,sizeof(test),1,result);
- 00000000: 00100000 01011000 01110101 00000010
- 00000000: 00100000 10100000 01110011 00000001
- struct R{
- char opcode[6];
- char rs[5];
- char rt[5];
- char rd[5];
- char shamt[5];
- char funct[6];
- };
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- int binpr(unsigned char Byte, FILE *f)
- {
- char buf[8];
- for(int i=0; i<8; i++) buf[i]=(char)(((Byte>>(7-i))&1)+'0');
- return (int)+fwrite(buf,1,8,f);
- }
- struct R{
- unsigned int opcode: 6;
- unsigned int rs: 5;
- unsigned int rt: 5;
- unsigned int rd: 5;
- unsigned int shamt: 5;
- unsigned int funct: 6;
- };
- int main()
- {
- struct R test = {32,0,11,21,19,0};
- system(": > result"); //rb requires that the file already exists
- FILE *fp = fopen("./result", "rb+");
- if(!fp) return perror("fopen"),1;
- if(1!=fwrite(&test,sizeof(test),1,fp)) return perror("fwrite"),1;
- rewind(fp);
- char buf[sizeof(struct R)];
- if(1!=fread(&buf,sizeof(buf),1,fp)) return perror("fread"),1;
- fputs(" ",stdout); if(0!=memcmp(buf,&test,sizeof test)) abort();
- for(size_t i=0; i<sizeof(test); i++) { binpr(*((unsigned char*)&test+i),stdout); fputs(" ",stdout); } puts("");
- system("xxd -b result |cut -d: -f2");
- /*OUTPUT:*/
- /*00100000 01011000 01110101 00000010 */
- /*00100000 01011000 01110101 00000010 Xu.*/
- }
- #include <stdio.h>
- #include <assert.h>
- static
- void format_binary8v(unsigned char x, int n, char buffer[static 9])
- {
- assert(n > 0 && n <= 8);
- int start = 1 << (n - 1);
- for (int b = start; b != 0; b /= 2)
- {
- *buffer++ = ((b & x) != 0) ? '1' : '0';
- x &= ~b;
- }
- *buffer = '