Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
His Divine Shadow
Aug 7, 2000

I'm not a fascist. I'm a priest. Fascists dress up in black and tell people what to do.
I'm working through learning C with the Dennis* Ritchie & Brian Kernighan book. Spent more time fiddling with the environment than programming the last few days however... I am on excersize 1.9. Been able to figure them out so far. I found a site with all the exercises as the books, in the same order. So I started going there afterwards to check how my solutions fare against the ones there.

This is how I approached the excersize of removing all double or longer blanks, and it works when I test it on a text file I got set up.


The site solution seems to be doing what I came up with though I think my syntax is more compact.
https://www.learntosolveit.com/cprogramming/ex_1.9_sinblank

I note they set up an initial value for the previous char as well as used a symbolic constant. I didn't do any of that since I didn't see the point. But is this a practice I should consider when doing C? Just trying to get good habits in from the start.

*I share the same name as the creator of C, that bodes well :)

His Divine Shadow fucked around with this message at 18:44 on Jan 1, 2023

Adbot
ADBOT LOVES YOU

His Divine Shadow
Aug 7, 2000

I'm not a fascist. I'm a priest. Fascists dress up in black and tell people what to do.
Thanks that makes sense, learned the importance of initializing variables.

His Divine Shadow
Aug 7, 2000

I'm not a fascist. I'm a priest. Fascists dress up in black and tell people what to do.
The book used ints in the examples and said this is because in a single character inside '' quotes is actually a small integer so it works with ints

Apparently you can use char, but chars don't like negative values, which might be returned by some non-ascii characters. So not a problem with ints, but apparently you can use a signed char to make it allow negative values. Or use ints?

My understanding might be flawed.

His Divine Shadow
Aug 7, 2000

I'm not a fascist. I'm a priest. Fascists dress up in black and tell people what to do.

csammis posted:

Learning C from K&R in the year 2023 seems really counterproductive unless your goal is explicitly to learn how C was written forty years ago. The modern C standard has more expressive power and safety than K&R had at the time.

That said I’ve been in C++ for long enough that I don’t know any modern resources for learning modern C, so. Maybe someone else here does?

I like this way personally. It's more interesting I find.

And I asked around the net before starting, asking about books and so, this was still recommended by loads of people to me. This is the 2nd edition which uses ANSI C which is very close to modern C that it should not be an issue.

His Divine Shadow
Aug 7, 2000

I'm not a fascist. I'm a priest. Fascists dress up in black and tell people what to do.
Is it bad form in c to write one over the other. From what I understand they should be equivalent since an array in c is just a pointer in disguise.

code:
int function(char var[]) {}

//instead of

int function(char var*) {}
Someone commented somewhere the 2nd was preferrable but I dunno why.

His Divine Shadow
Aug 7, 2000

I'm not a fascist. I'm a priest. Fascists dress up in black and tell people what to do.

Ihmemies posted:

This course would probably be easier, if it existed in some real form. Now it is a self-study course consisting of ~80 exercises, a 155-page PDF and an exam. No lectures, no assistance or anything really. Just a bunch of old slides from year X and a bunch of excericses you have to do.

I thought the bitwise stuff look neat and interesting from what little I've seen of them so far.

This guy explained the concept well for how you'd use bit fields and it sounds very useful to me.

https://www.youtube.com/watch?v=aMAM5vL7wTs

I went and studied hex to binary tables a little after this, made it easier to follow the video.

His Divine Shadow fucked around with this message at 13:48 on Feb 6, 2023

His Divine Shadow
Aug 7, 2000

I'm not a fascist. I'm a priest. Fascists dress up in black and tell people what to do.
I don't have a question but I just want to say the bitwise exercise in the C book I got is hard and makes me feel stupid. I had to go look at the solution and looked at alternative solutions and feel like I am barely hanging on. Hopefully I'll get there.

His Divine Shadow
Aug 7, 2000

I'm not a fascist. I'm a priest. Fascists dress up in black and tell people what to do.
I have a function that writes it out on screen for me actually. Lifesaver. Though there might be some merit to writing on paper.

His Divine Shadow
Aug 7, 2000

I'm not a fascist. I'm a priest. Fascists dress up in black and tell people what to do.
Well here's how I attempted to do exercise 2-7 in my C book. I have not looked at the answer yet but I feel pretty sure it's radically different, more compact. I do believe this code is pretty inefficient and can be optimized a lot and most of the x* variables can be removed but I wrote out it out like this to make it simpler to see and understand what's happening. The exercise is that I want to flip or invert any bits, starting at position p as well as n bits to the left of p. Leaving the rest unchanged.

My idea was to make two bit masks to mask of the area that is to be flipped, combine that into a new mask and then use said mask with a XOR operator on the original. I've tested it on a couple of numbers and it seems to work.





I noticed I have to use p-1 to start from bit 4 and not 5, which OK I can see that might work, it counts from 1 not 0 I guess. But then (p-1)+n doesn't yield the correct offset. There I have to remove the -1 . I'm still not sure why and I don't like it.

His Divine Shadow
Aug 7, 2000

I'm not a fascist. I'm a priest. Fascists dress up in black and tell people what to do.

leper khan posted:

I would do something like

code:
uint32_t invert(uint32_t value, uint8_t position, uint8_t window_size) {
  uint32_t window_mask = ((1 << window_size) -1) << position;
  return value ^ window_mask;
}
Though there's likely something a bit more efficient in the bit twiddling hacks document.

Is there an advantage to doing it that way?

code:
 x2 = x ^ ((INT_MAX << (p - 1)) ^ (INT_MAX << (p+ n)));
I condensed my original to a single line anyway so I feel happy with it, unless it's terribly wrong somehow.

His Divine Shadow
Aug 7, 2000

I'm not a fascist. I'm a priest. Fascists dress up in black and tell people what to do.

leper khan posted:

Fewer arithmetics. Readability. Real answer requires perf testing I'm too lazy to do right now for you :effort:

Satisfied with that reasoning, to my eyes I understood my own code more easily at first but it was fresh in my head.

FWIW they don't produce the same results, I'm picking over it now as an another exercise.

His Divine Shadow
Aug 7, 2000

I'm not a fascist. I'm a priest. Fascists dress up in black and tell people what to do.

rjmccall posted:

I mean, your implementation is flipping N+1 bits and Leper’s is flipping N, that’s the obvious difference.

I did some + and - on the variables and now they both come out identical with the same data fed to them, so I guess what you said.

All this still feels obscure and hard to wrap my head around, even the easy to read function. Oh well onto exercise 2.8, wtf does it mean to rotate a bit? That was a rhetorical question. I already googled it. This actually looks easier though, I think I know how to do this... We'll see.

His Divine Shadow
Aug 7, 2000

I'm not a fascist. I'm a priest. Fascists dress up in black and tell people what to do.
Eh, you can go older, something I did for fun

His Divine Shadow
Aug 7, 2000

I'm not a fascist. I'm a priest. Fascists dress up in black and tell people what to do.
I was gonna ask a question but I found the answer myself, it was why was this function I had simply copied and used earlier, was using malloc when it hardcoded the length anyway. But as I found here:

quote:

https://www.programmingsimplified.com/c/source-code/c-program-convert-decimal-to-binary
We allocate memory dynamically because we can't return a pointer to a local variable (character array in this case). If we return it to a local variable, then the program may crash, or we get an incorrect result.

I do have a follow up however.

So I was using this function directly in a printf statement to format an integer, I never assigned it to a variable. By doing that I assume I have created a memory leak.

So the right way to handle this is to assign the return value to a variable that I can free later right? I guess it's not possible for a function that calls malloc to clean up after itself either or it would erase the pointer it was supposed to return.

His Divine Shadow
Aug 7, 2000

I'm not a fascist. I'm a priest. Fascists dress up in black and tell people what to do.
You mean like a function that takes a length and int, like say something like this

code:
char binvar[5];
binvar = decimal2bin(5, 12);
Get back 1,1,0,0,'\0' ?

His Divine Shadow
Aug 7, 2000

I'm not a fascist. I'm a priest. Fascists dress up in black and tell people what to do.
Oh yeah, gotcha.

His Divine Shadow
Aug 7, 2000

I'm not a fascist. I'm a priest. Fascists dress up in black and tell people what to do.
C++ code looks so loving incomprehensible to me compared to regular C.

Adbot
ADBOT LOVES YOU

His Divine Shadow
Aug 7, 2000

I'm not a fascist. I'm a priest. Fascists dress up in black and tell people what to do.
Reading a few pages back about CS and universities and I realize I will never ever be able to do any of that, it all sounds dreadfully boring. I'm also too old. I only do some programming stuff because it's interesting, but the higher education scene sounds like aliens talking about concepts beyond my feeble brain.

His Divine Shadow fucked around with this message at 08:15 on Mar 15, 2024

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply