bytes

mail@pastecode.io avatar
unknown
abap
2 years ago
3.4 kB
2
Indexable
Never
#include<stdio.h>
enum valori { alfa = 0, beta = 1, gama = 2, delta = 3, epsilon = 4 };
int Leds = 0xAB; //10101011

int ex_switch(int n); //exercitiu 3
int ex_elseif(int n); //exercitiu 3
int ex_factorialFor(int n); // exercitiu 4
int ex_factorialWhile(int n);
void printbits(int input);
unsigned char SetByteBit(unsigned char Byte, unsigned char Bit);
unsigned char ResetByteBit(unsigned char Byte, unsigned char Bit);

#define LED0 0x01
#define LED1 0x02
#define LED2 0x04
#define LED3 0x08 
#define LED4 0x10
#define LED5 0x20
#define LED6 0x40
#define LED7 0x80

/*
#define LED0 0x01
#define LED1 0x02
#define LED2 0x04
#define LED3 0x08 
#define LED4 0x16
#define LED5 0x32
#define LED6 0x64
#define LED7 0x128
*/


int main()
{
    int n = 0;
    unsigned char LED_States = 0x00;
    unsigned char val, i;

    printf("The values of the LEDs before: \n");
    for (i = 0; i <= 7; i++) {

        val = ((0x01 << i) & LED_States) >> i;
        printf("%d", val);
    }

    //LED_States &= (~(LED3 | LED4));

    val = SetByteBit(LED_States, LED2);
    val = SetByteBit(LED_States, LED6);

    printf("\n The final values of the LEDs: ");

    for (i = 0; i <= 7; i++)
    {
        val = ((0x01 << i) & LED_States) >> i;
        printf("%d", val);
    }
    //scanf_s("%d", &n);

    //printf("%d", ex_switch(n));
    //printf(" %d", ex_elseif(n));
    //printf(" %d", ex_factorialFor(n));
    //printf(" %d", ex_factorialWhile(n));

    //Leds |= MASK_LED2AND6_ON;    // set bit
    //Leds &= MASK_LED3AND4_OFF; // clear bit

    //Leds |= 1 << x;    // set bit
    //Leds &= ~(1 << x); // clear bit
    //SetByteBit( Byte, Bit)
   // ResetByteBit(Byte, Bit)

    //printbits(Leds);
    return 0;
}

int ex_switch(int n)
{
    int returnVal;
    switch (n)
    {
    case alfa:
    {
        returnVal = (n + n);
        break;
    }
    case beta:
    {
        returnVal = (n * n);
        break;
    }
    case gama:
    {
        returnVal = (n * n);
        break;
    }
    case delta:
    {
        returnVal = (n + n + n * n);
        break;
    }
    case epsilon:
    {
        returnVal = (n + n * n * n);
        break;
    }
    default:
    {
        //do nothing
    }
    }

    return returnVal;
}

int ex_elseif(int n)
{
    int returnVal;
    if (n == alfa) {
        returnVal = (n + n);
    }
    else if (n == beta) {
        returnVal = (n * n);
    }
    else if (n == gama) {
        returnVal = (n * n);
    }
    else if (n == delta) {
        returnVal = (n + n + n * n);
    }
    else if (n == epsilon) {
        returnVal = (n + n * n * n);
    }
    else {
        //do nothing
    }

    return returnVal;
}

int ex_factorialFor(int n)
{
    int factorial = 1, i = 0;
    for (i = 1; i <= n; i++)
    {
        factorial *= i;
    }

    return factorial;
}

int ex_factorialWhile(int n)
{
    int factorial = 1;
    while (n != 0)
    {
        factorial *= n;
        n--;

    }

    return factorial;
}




unsigned char SetByteBit(unsigned char Byte, unsigned char Bit) {

    Byte = ((0x01 << Bit) | Byte);
    Byte = (0x01 << 7) | Byte;

    return Byte;
}

unsigned char ResetByteBit(unsigned char Byte, unsigned char Bit) {

    Byte = (~(0x01 << Bit)) & Byte;

    return Byte;
}