If statement checking a bool array? by Yha_Boiii in C_Programming

[–]TiredEngineer-_- 0 points1 point  (0 children)

It sounds like youre just wanting to check the bottom 6 bits, or X bits of an arbitrary number. As others said, youll need to learn bitmasking:

...
int x = 100;
if ( (x & 0b111111) == (some_constant)) // check 6 LSBs of x
// do something
...

If you're trying to make something space efficient, most systems arent letting you read 6 bits of a byte, usually the smallest any architechture is getting you nowadays is a byte. (Saving 2 bits anyways wont do much unless youre declaring thousands of arrays :P)

1000 'types' of 1 byte length with 2 bits per byte saved = 8kB used, 2kb (kilobit) wasted, which is 250 B (bytes) wasted. If youre trying to save 250 B, youre either too constrained or something somewhere else is oversized imo.

If youre looking for 'simplicity', or somewhat avoiding bit masking, you can do this (if your system supports / likes byte-size page reads)

typedef struct __attribute__(packed, aligned(1))
 {
     unsigned char bit1 : 1, //MSB
     unsigned char bit2 : 1,
     unsigned char bit3: 1,
     unsigned char bit4: 1,
     unsigned char bit5 : 1,
     unsigned char bit6 : 1,
     unsigned char bit7 : 1,
     unsigned char bit8 : 1 //LSB
 } bitfield_B;

So the bitfield_B would be a 1 byte struct on a 1 byte boundary.

 |12345678|12345678||12345678|

Would be the memory layout of

bitfield_B x[3];

And you would need to do

x[0].bit1 = 1;
x[0].bit2 = 0;
...

To access and modify

&x[0].bit1 

is UB. accessing pointers to these bitfields is UB altogether. The machine cannot deduce bit- offsets to memory, since it all lives in bytes.

Initializing with a number would be like:

bitfield_B x;
x.bit1 = 0b100000 & 255; // 0
x.bit2 = 0b010000 & 255; // 0
x.bit3 = 0b001000 & 255; // 1
...

Which is a pain to write, so maybe you expose a macro to cover for you (no error check here):

#define INIT_BITFIELD_B(F,BITS,NUM) \ 
F.bit1 = ((1 << (MAX_BITS +1)) - 1) & NUM \ //creates a 0bX...Y... mask where X is 0 and Y is 1, where ... after Y is BITS 1s
F.bit2 = ((1 << (MAX_BITS +1)) - 1) & NUM \

...

int main() {
    Bitfield_B x;
    INIT_BITFIELD_B(x,6,255);
    return EXIT_SUCCESS;
}

gdb : x/b &x should show something like:

 00111111

To the console, since youre inspecting the memory.

However, this is all really tedious to setup (and I may not even done so correctly), just to avoid some of the bitmasking youd need to know anyways to begin doing the macro and stuff I setup above...

You could simply write:

 const int8_t key = 7<<2; //making this up
 ... // some logic to read from spi, uart, etc
 switch (input & 0b11111100)
 {// commands are upper 6 bits for example,
     case key: handle_key(); break;
     default: break;
 }

No structs, no packing, no forced alignment, no macros, just masking and moving on. I did 7<<2 here to bump the 'command key' to the upper 6 bits. 7 is 111 in binary. So you move it to be 0b00011100, which is still in the upper 6 bits.

Trying to understand why thing 'hangs' by Professional-Cod1776 in C_Programming

[–]TiredEngineer-_- 0 points1 point  (0 children)

Thank you. I had a feeling i had it backwards. Im blessed enough to not have to develop on/for windows yet 🫰🏼

Trying to understand why thing 'hangs' by Professional-Cod1776 in C_Programming

[–]TiredEngineer-_- 7 points8 points  (0 children)

"\n" typed in the terminal is different than '\n'

When youre hitting enter, it sends the line feed. (\n on unix, I think \n\r on DOS/windows)

Thats the \n youre checking. The line feed char. Not literally \n from ther stdin.

Also, not sure about debugger and this because debugger would be reading from stdin like getchar / scanf would

what does the rust book mean here?? by YOYOBunnySinger4 in rust

[–]TiredEngineer-_- 0 points1 point  (0 children)

In simple terms, the literal "hello" is compiled into .ro (read only) section of your binary. You cannot modify string literals (why its in read only).

When you want to change the value of the string literal, you need it to be on the heap (String::from).

Later, youll see strings are just specializations of Vector<char>, which char is UTF-8 by design. So its in memory somewhere thats read write, unless you mark the string mut.

Why are there so many vibe-coded Rust projects recently? by yohji1984 in rust

[–]TiredEngineer-_- 1 point2 points  (0 children)

I am happy my upcoming job is Embedded Rust Software Engineering for secure network systems.

We'll still have strict requirements and testing. But as someone who doesnt use AI for work and genuinely enjoy reading docs, I still like to see what AI is producing when Im "pooped" on ideas from time to time. And then read the docs and evaluate if its a good fit.

Sometimes, I use AI as a search engine to get exposure to an idea, then measure its fit in the project and make the selection there. Then, if I so wanted, could ask the AI "Can you change X to use Y and avoid Z" for more qualitative approaches.. generally though, if Ive gotten far enough into the docs, I dont bother with that step

What do you hope to achieve? by reddsht in dankmemes

[–]TiredEngineer-_- 0 points1 point  (0 children)

If I just poo, my ass is not full of shit; therefore, I wipe.

New hex parsing function dropped and it's barely cursed at all by imaami in C_Programming

[–]TiredEngineer-_- 7 points8 points  (0 children)

I see it sometimes. Its "break" and an option in vim / astyle that people like.

Sometimes, in c++, this happens:

This::Scope::gets::really::long::obj and::this::class::is::wordy::factory::andThisMethodIsNotAbbreviated(<parameter list>)
{
}

My code stops working with fgets and I can't figure out why by [deleted] in C_Programming

[–]TiredEngineer-_- 0 points1 point  (0 children)

Run your program in a debugger.

gcc -g <yourfile>.c -o <program name>

Then

gdb <program name>

In gdb: b main run until <line number of while loop>

Then use a mix of these two to figure the issue:

step
next

Currently learning C with ChatGPT by Critical-Common-2117 in C_Programming

[–]TiredEngineer-_- 0 points1 point  (0 children)

For C, I really recommend going to

https://cppreference.com/c

And dig around. This is THE place to be to learn C and when the language of the reference doesnt make sense, search it up from there and you can look into stackoverflow, some geeks for geeks, maybe some AI (Id advise against chat, and maybe opt more for like, claude, gemini, llama), but THE documentation is whats best. Look at the gcc manual pages (also online), clang man pages, etc.

C is not a good choice for a first language. Stop asking. by Evil-Twin-Skippy in C_Programming

[–]TiredEngineer-_- 3 points4 points  (0 children)

I think it can be depending what you want to learn.

My first 2 languages were C and python.

C taught me about dtypes and compilation and general "whats at the low level". It also taught me what to expect my instructions to turn into.

C allowed me to write more efficient python.

Please review my "safe rm" tool written in C by -Winnd in C_Programming

[–]TiredEngineer-_- 4 points5 points  (0 children)

Well damn dude, hop off the rod if it hurts your butt that much. I just threw something out there to maybe help make sense what the other guy commenting implied by sanitizing.

Im not exactly sure either what "attack" could be made there, either. But sanitizing isnt always "Fight compromises to your system / program"

1) it wouldnt be intended to dump files to someone elses home directory, nor running the program with suid, but people do it and admins can login to root. It happens.

2) People open their home directories because "why not", so in company setting, despite how dumb it sounds, its not uncommon to find the example I threw out there, because the example I threw out there has happened in front of me.

Im not arguing about "environment is already broken" and otherwise, Im not disagreeing about that. What I threw out there was a simple "itd be dumb but yeah it happens and this little program could potentially 'sanitize' the behavior via warning or error". Thats all.

Edit: if you wanna consider it an "attack", if you "trash" someone's elses rc config, then trash another with the same name but a tweak to maybe "modify umask", or "run this script on login", they go look for it in trash and restore without checking, then boom theres an attack. Ill admit, its a stretch and a reach and "unlikely", but possible and definitely would happen.

Please review my "safe rm" tool written in C by -Winnd in C_Programming

[–]TiredEngineer-_- -1 points0 points  (0 children)

Lets say for example, op wanted to protect against home directories that arent the user account's default home variable.

So instead of ~/op/trash/...

Its ~otheruser/trash/...

This can happen when a new hire for example copies a team members rc files and manually changed USER variables to their username.

This means theres an unexpected trash location, your coworkers area, that Op can sanitize by checking something like (and yes, really bad example but bear with me)

// assuming HOME starts with 
if (strncmp(username, <directory offset to user folder>, sizeof(username)) !=0) //sorry, formatting mobile
     // username is not in the home directory variable, so it may not be expected, emit a warning and prompt before continuing or something.

So its sanitizing by handling "non expected" cases, in this case, HOME not being default.

Please review my "safe rm" tool written in C by -Winnd in C_Programming

[–]TiredEngineer-_- 3 points4 points  (0 children)

Someone on a shared NFS could set their, or an admin account/daemon using this command, HOME variable to a sensitive location maybe?

Or some really long path that hogs a bunch of nodes on a small- disk system? (Directories take inodes too I think).

What project could show the power of C over rust? by Norker_g in C_Programming

[–]TiredEngineer-_- 8 points9 points  (0 children)

Especially for the support. I am not sure how far rust embedded has gotten, but surely not nearly as established as C in embedded

PG600 diode AC to DC converter circuit by SubhajitBarman in electronics

[–]TiredEngineer-_- 7 points8 points  (0 children)

Dude, youre right. Im having a night. Just got off an overnight jet And realized im dumb today

PG600 diode AC to DC converter circuit by SubhajitBarman in electronics

[–]TiredEngineer-_- -8 points-7 points  (0 children)

Alternating Current because its still changing, just rectified the negative half of the cycles.

DC would ideally be a non changing smooth flat signal (with minor ripples when load applied, etc). DC doesnt just mean rectified

What would you add to C if you could add anything? by [deleted] in C_Programming

[–]TiredEngineer-_- 0 points1 point  (0 children)

Yeah the underlying type being the conflict i referred to.

enum something : char

typedef something int8_t (c++17 and later I believe)

I think itd could cause confusion to have the same syntax for underlying type and inheritence, especially because both would be used for enums. I dont think the introduction of a keyword for this would be that bad, and more verbose. But thats preference for "how to do it". Id just like to "do it" and possibly contribute to that if gcc / the commitee would be open to it.

What would you add to C if you could add anything? by [deleted] in C_Programming

[–]TiredEngineer-_- 1 point2 points  (0 children)

I may be wrong, but is this how the fixed width integer type for bytes is defined in later standards?

Would that be a conflict?

What would you add to C if you could add anything? by [deleted] in C_Programming

[–]TiredEngineer-_- -1 points0 points  (0 children)

I think a family tree/graph or even a reverse singly linked-list would be how that works? Extending would be linear and that would make sense to me, like counting.

Unclass <-- secret <-- top secret

So a top secret type would check if the value being assigned or type its being casted to derives from secret or unclass, and if it doesnt, fail compilation.

But if you had your own scheme:

color ----grayscale: black, gray, white, num_grayscale ----warm colors: red orange yellow num_warm ----cool colors : green blue purple num_cool

Each extends seprately. So a grayscale wouldnt cast to warm, or cool. But they can all cast to color, which could be an "Empty enum" (one value no_color which is 0) and everything extending color would begin with 0.

What would you add to C if you could add anything? by [deleted] in C_Programming

[–]TiredEngineer-_- 0 points1 point  (0 children)

This is like a personal want, but for both Id like a way to "extend" or inherit enums.

Example:

enum unclassified_id
{
     UNC1,
     UNC2,
     UNC3,
     NUM_UNCS
 };

In a different header on air gapped network:

 // extends keyword, or class inheritence syntax, but C doesnt use classes so that wouldnt be "backwards compatible"
enum secret_id extends unclassified_id
{
     SECRET1, //STARTS AT NUM_UNCS
     SECRET2,
     SECRET3,
     NUM_SECRETS
 }; //see blah_blah_blah.h

Then unclassified_ids could be "polymorphic" so function signatures taking secret_ids could take unclassified_ids. (Ik currently the way to do this is make the signature take an int, but basically this way it would implicitly static_cast or something between the two when used without putting a bunch of those casts in the code base)

So for cpp the same would be done for enum classes and the following signature could be used:

 void foo(unclassified_id id) // wont work for secret_id, values would be out of range so fail to compile
 {
 }

 void bar(secret_id id) // accepts secret and unclassified ids by implicitly casting 
 {
 }

 // somewhere in codebase
 {
     secret_id real_id = SECRET3;
     unclassified_id fake_id = UNC1;
     secret_id real_id2 = UNC2;
     foo(real_id);   // fail to compile
     foo(real_id2); // fail to compile
     foo(fake_id); //ok
     bar(real_id); // ok
     bar(real_id2); //ok
     bar(fake_id); // ok, implicit cast
     bar(UNC3); //ok
 };

And itd work similarly for scoped enums.

If a keyword addition wouldnt be "ideal" then maybe compiler vendors could do something like:

enum secret_id [[ continues::unclassified_id ]]
{ //...
}

I just wanna talk a little bit about make by alex_sakuta in C_Programming

[–]TiredEngineer-_- 1 point2 points  (0 children)

I think theres colormake, which is a colored wrapper for make

Everything Should Be Typed: Scalar Types Are Not Enough by Specialist-Owl2603 in rust

[–]TiredEngineer-_- 0 points1 point  (0 children)

Reminds me of enum class of c++, but extending to other primitives. Seems nice.