Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdint.h>
- #include <stdalign.h>
- #define N 5
- typedef struct Var_Msg{
- char c;
- int n;
- char d;
- }Var_Msg_t;
- typedef struct Fixed_Msg{
- unsigned char mid;
- char data[10];
- }Fixed_Msg_t;
- typedef union Mesage{
- Var_Msg_t *variable_length_message;
- Fixed_Msg_t *fixed_length_message;
- }Message_t;
- union Test{
- char c;
- unsigned n;
- float f;
- } uniune;
- int v[10];
- unsigned char parityBit(unsigned char n){
- // B7 B6 B5 B4 B3 B2 B1 B0
- // 0 0 1 1 1 0 1 1 //OddParity
- unsigned ones=0;
- for(int i=1; i<=7; i++){
- if ((n&1u)!=0){
- ones++;
- }
- n=n>>1;
- }
- return ones;
- }
- unsigned char setParityBit(unsigned char n){
- unsigned char parityBitValue=parityBit(n)%2;
- n=n&(~(1u<<7u)); //pune fortat pe 0 bitul 7
- n=n|(parityBitValue<<7u); //pune bitul 7 pe valoarea bitului de paritate
- return n;
- }
- /* sa se returneze valoarea maxima care se poate obtine
- * prin deplsasari cu recirculare la stanga a bitilor
- * unui argument de tip unsigned
- */
- unsigned shLRot(unsigned n){
- unsigned nBits=sizeof(n)*8;
- unsigned msb=n&(1u<<(nBits-1)); //scoate msb
- n<<=1; //n=n<<1; //muta la stanga 1 poz
- n=n|(msb>>(nBits-1)); // lsb(n)<-msb
- //printf("0x%x\n", n);
- return n;
- }
- unsigned maxShLRot(unsigned n){
- // 01...00011001001
- // 1...000110010010
- // ...0001100100101
- // ..0001100100101.
- // .0001100100101..
- // 0001100100101...
- // 001100100101...0
- unsigned max=n;
- for (unsigned i=0; i<sizeof(n)*8; i++){
- unsigned nou=shLRot(n);
- printf ("%x %x\n", n, nou);
- if (nou>max){
- max=nou;
- }
- n=nou;
- }
- return max;
- }
- int stergeElem(const int t[], int* n, int poz){
- // /* ret 0 daca e ok si valoare negativa pt eroare */
- // if (poz<0 || poz>n-1) return -1;
- // for (int i=poz; i<(*n-1); i++){
- // t[i]=t[i+1];
- // }
- // *n=*n-1;
- // //(*n)--;
- }
- void stergePare(int t[], int* n){
- for (int i=0; i<*n; i++){
- if (t[i]%2==0){
- stergeElem(t, n, i);
- i--;
- }
- }
- }
- void printTab(int t[], int n){
- printf("\n");
- for (int i=0; i<n; i++){
- printf("%d ", t[i]);
- }
- printf("\n");
- }
- int eSimetric(int t[], int n){
- // printf("Marimea lui t2 este=%u\n", sizeof(t));
- /* elem egal departate de capate sunt identice */
- int ok=1;
- for (int i=0; i<=n/2 && ok; i++){
- if (t[i]!=t[n-1-i]){
- ok=0;
- }
- }
- return ok;
- }
- void f(int w[10]){
- printf("Sizeof de v in fct este %d\n", sizeof(v));
- printf("Nr de eleme in fct sete %d\n", sizeof(v)/sizeof(v[0]));
- printf("Sizeof de v in fct este %d\n", sizeof(w));
- printf("Nr de eleme in fct sete %d\n", sizeof(w)/sizeof(w[0]));
- }
- void handleCommand(char *cmd) {
- /* led, 1, ON */
- // char cuv[10]={0};
- // // memset(cuv,0,10);
- // char *pv=strchr(cmd,',');
- // strncpy(cuv, cmd, pv-cmd);
- // cuv[pv-cmd]='\0';
- char* cuv=strtok(cmd,", ;");
- printf("Cuvant este %s\n", cuv);
- if (strcmp(cuv, "led") == 0) {
- printf("cautam care led\n");
- char* ledId = strtok(NULL, ", ;");
- uint8_t ledNr = atoi(ledId);
- printf("Ledul numarul %d\n", ledNr);
- if (ledNr <= 2 - 1) {
- char *actiune = strtok(NULL, ", ;");
- if (strcmp(actiune, "ON") == 0) {
- // STM_EVAL_LEDOn(1);
- printf("Aprinde led %d\n",ledNr);
- }
- if (strcmp(actiune, "OFF") == 0) {
- printf("stinge led %d\n",ledNr);
- }
- }
- }
- }
- typedef union {
- uint32_t word;
- uint8_t bytes[4];
- } word_msg_t;
- typedef struct DateTime{
- unsigned y:6;
- unsigned m:4;
- unsigned d:5;
- unsigned h:5;
- unsigned mm:6;
- unsigned ss:6;
- }DateTime_t;
- char* expandeazaSir(char *s){
- int n=1;
- char *rez=malloc(n* sizeof(char));
- if(rez==NULL){
- printf("nu s-a reusit alocarea");
- return NULL;
- }
- rez[n-1]='\0';
- int i=0;
- while(s[i]!=0 ){
- if (isalpha(s[i])){
- char c=s[i];
- // printf("caracterul este %c\n", c);
- i++;
- int nAp=0;
- nAp=atoi(s+i);
- // while(isdigit(s[i])){
- // nAp=nAp*10+(s[i]-'0');
- // i++;
- // }
- // for (int j=1; j<=nAp; j++){
- // printf("%c",c);
- // }
- for (int j=1; j<=nAp; j++){
- n++;
- rez=realloc(rez,100*n*sizeof(char));
- if (rez==NULL){
- printf("Nu s-a reusit realocarea");
- return NULL;
- }
- rez[n-2]=c;
- }
- rez[n-1]='\0';
- }
- i++;
- }
- //printf("Rez este %s\n", rez);
- return rez;
- }
- int main(){
- // char* s=expandeazaSir("g5i2g32e2l1a3");
- // if(s!=NULL){
- // printf("%s",s);
- // free(s);
- // }
- unsigned long long q=1;
- char *aux=expandeazaSir("g5i2g32e2l1a3");
- char *s=aux;
- while(s!=NULL){
- if (q%10000==0) printf("%d\n", q);
- s=expandeazaSir("g5i2g32e2l1a3");
- if (s!=NULL){
- aux=s;
- }
- else{
- printf("Ultimul nenull: %p\n", aux);
- }
- q++;
- }
- printf("Am reusit sa aloc de %d ori\n", q);
- printf("Ultimul aux este %p\n", aux);
- free(aux);
- printf("gata");
- printf("\n\n\n\n");
- unsigned x=1547710819;
- DateTime_t date=*(DateTime_t*)&x;
- // date.d=17;
- // date.m=1;
- // date.y=2019-1970;
- // date.h=10;
- // date.mm=4;
- // date.ss=0;
- printf("%u-%u-%u, %u:%u:%u\n", date.y+1970, date.m, date.d, date.h, date.mm, date.ss);
- word_msg_t test;
- //test.word=(0xAB)<<24|(0xCD<<16)|(0x12<<8)|(0x34);
- test.word=0xABCDE1234;
- // test.bytes[0]=0x34;
- // test.bytes[1]=0x12;
- // test.bytes[2]=0xCD;
- // test.bytes[3]=0xAB;
- printf("%x\n", test.bytes[0]);
- printf("%x\n", test.word&0xFF);
- uniune.f=3.14;
- printf("IEEE in hexa este 0x%x\n", uniune.n);
- printf("Nr este %d\n", uniune.f);
- printf("\n\n\n\n");
- printf("Marimea ste %d\n", sizeof(Var_Msg_t));
- printf("Numarul de biti de aliniament este %d\n", _Alignof(Var_Msg_t));
- //char s[10]="led,1,ON";
- handleCommand(s);
- while(1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement