Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
AC_setting_example
//gcc 5.4.0 // Imagine an AC system that is controlled using an 8-bit number: // The lowest two bits of the number show the fan setting: 00 is for off, 01 is for low, 10 is for medium and 11 is for high. // The second two bits of the number show the humidity setting: 00 is for low, 01 is for medium, and 10 is for high. 11 is not an option. // The upper 4 bits show the temperature setting. 60 degrees is represented as 0000, 61 as 0001 and so on. The highest temperature setting is 75 as 1111. // The following functions are written to set, update or print this AC's settings #include <stdio.h> // We use enum and define so that all the setting values are in one place at the beginning of the program // We try to avoid hard-coding actual numbers inside the code as much as possible // For each of the below masks, all the bits are 0 except for the specific bit mentioned #define BIT0_MASK 1 #define BIT1_MASK (1<<1) #define BIT2_MASK (1<<2) #define BIT3_MASK (1<<3) #define BIT4_MASK (1<<4) #define BIT5_MASK (1<<5) #define BIT6_MASK (1<<6) #define BIT7_MASK (1<<7) #define BIT8_MASK (1<<8) enum FAN_SETTINGS {FAN_OFF, FAN_LOW, FAN_MED, FAN_HGH}; // The mask for the fan has 1 for the lowest two bits and 0 for the rest #define FAN_MASK (BIT0_MASK | BIT1_MASK) // This function prints the AC setting in this format: // "Fan is ... Humidity is ...Temperature is ..." void printACSetting(int ACSetting) { int fanSetting = ACSetting & FAN_MASK; switch(fanSetting) { case FAN_OFF: printf("Fan is off."); break; case FAN_LOW: printf("Fan is low."); break; case FAN_MED: printf("Fan is medium."); break; case FAN_HGH: printf("Fan is high."); break; } // Write the rest of printing function for humididty and temperature return; } // fanSetting is a number between 0 and 3 // This functions returns the new ACSetting where all is the same except for the new fanSetting int updateFanSetting(int ACSetting, int fanSetting) { // FAN_MASK in binary is 11. This means it has a 1 exactly where fan setting bits are // inverted mask will have 1 in all bits except for fan setting bits. // Note that ! operation is logical operation and is not bitwise, so it cannot be used here. // Instead, we use ~ which does bit-wise inversion int invertedFAN_MASK = ~FAN_MASK; printf("\n%x\n", invertedFAN_MASK); //This clears the fan setting bits int updatedACSetting = ACSetting & invertedFAN_MASK; //This puts fanSetting and the rest of AC setting together updatedACSetting = updatedACSetting | fanSetting; return (updatedACSetting); // We wrote the above code for maximum readability. Here is the short version of the same code // return( ((ACSetting & ~FAN_MASK) | fanSetting); } // humiditySetting is a number between 0 and 2 // This functions returns the new ACSetting where all is the same except for the new hunmiditySetting int updateHumiditySetting(int ACSetting, int humiditySetting) { } // tempSetting is a number between 60 and 75 // This functions returns the new ACSetting where all is the same except for the new tempSetting int updatetempSetting(int ACSetting, int tempSetting) { } int readFanSetting() { int fanSetting; printf("\nEnter fan setting: \n"); scanf("%d\n", &fanSetting); if (fanSetting < FAN_OFF) fanSetting = FAN_OFF; else if (fanSetting > FAN_HGH) fanSetting = FAN_HGH; return(fanSetting); } int main(void) { int ACSetting = 0x46; printACSetting(ACSetting); int fanSetting = readFanSetting(); ACSetting = updateFanSetting(ACSetting, fanSetting); printACSetting(ACSetting); return 0; }
run
|
edit
|
history
|
help
0
18BCE2182 ASSESS_1 Q1-1
Project 3 Part 1 v1.4
3D NO SE XD
ㅇㅇㅇ
Link List
Linked List
hello world
Don't ignore warnings
14th Dec Project1 v0.2
LECTURA DE CADENAS CON FGETS