Posts

Showing posts with the label binary

C read binary stdin

C read binary stdin I'm trying to build an instruction pipeline simulator and I'm having a lot of trouble getting started. What I need to do is read binary from stdin, and then store it in memory somehow while I manipulate the data. I need to read in chunks of exactly 32 bits one after the other. How do I read in chunks of exactly 32 bits at a time? Secondly, how do I store it for manipulation later? Here's what I've got so far, but examining the binary chunks I read further, it just doesn't look right, I don't think I'm reading exactly 32 bits like I need. char buffer[4] = { 0 }; // initialize to 0 unsigned long c = 0; int bytesize = 4; // read in 32 bits while (fgets(buffer, bytesize, stdin)) { memcpy(&c, buffer, bytesize); // copy the data to a more usable structure for bit manipulation later // more stuff buffer[0] = 0; buffer[1] = 0; buffer[2] = 0; buffer[3] = 0; // set to zero before next loop } fclose(stdin); How do I read in 32 bits at a ti...

Why is my logical mask not working on a 2D matrix in matlab properly?

Why is my logical mask not working on a 2D matrix in matlab properly? X(100,371) %% contains 100 datapoints for 371 variables I want to keep only the data which are within mean+standard deviation:mean-standard deviation. This is how I am proceeding: mx=mean(X);sx=std(X); %%generate mean, std %%this generates mx(1,371) and sx(1,371) mx=double(repmat(mx,100,1)); %%this fills a matrix with the same datapoints, %%100 times sx=double(repmat(sx,100,1)); %% this gives mx(100,371) and sx(100,371) g=X>mx-sx & X<mx+sx; %%this creates a logical mask g(100,371) %%filled with 1s and 0s test(g)=X(g); %%this should give me test(100,371), but I get %%test(37100), which is wrong as it doesnt maintain %%the shape of X test=reshape(test,100,371) %% but when I compare this to the my original matrix %% X(100,371) I hardly see a difference (datapoints %% in test are still outside the range I want. What am I doing wrong? 1 Answer ...