Bitmanipulation

Committer:
robertbuc
Date:
Sun Jan 12 22:33:32 2020 +0000
Revision:
0:56207c59a56d
Lekt2_Bitmanipulation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
robertbuc 0:56207c59a56d 1 #include "mbed.h"
robertbuc 0:56207c59a56d 2
robertbuc 0:56207c59a56d 3 /*
robertbuc 0:56207c59a56d 4 [SetDelTogBits] Schreibe ein Programm das folgende Tasks beinhaltet:
robertbuc 0:56207c59a56d 5
robertbuc 0:56207c59a56d 6 //MSB LSB
robertbuc 0:56207c59a56d 7
robertbuc 0:56207c59a56d 8 Lösche (0) das LSB (least significant bit), MSB (most significant bit) beim Datentype uint8_t.
robertbuc 0:56207c59a56d 9 Setze (1) das LSB (least significant bit), MSB (most significant bit) beim Datentype uint16_t.
robertbuc 0:56207c59a56d 10 Toggle (0 wird 1 bzw. 1 wird 0) das LSB (least significant bit), MSB (most significant bit) beim Datentype uint32_t.
robertbuc 0:56207c59a56d 11 Lösche/Setze/Toggle das 3. und das 5. Bit beim Datentype uint8_t.
robertbuc 0:56207c59a56d 12 */
robertbuc 0:56207c59a56d 13 BusOut myleds(LED1, LED2, LED3, LED4);
robertbuc 0:56207c59a56d 14
robertbuc 0:56207c59a56d 15
robertbuc 0:56207c59a56d 16 void print_binary(uint8_t x) { // function to print binary
robertbuc 0:56207c59a56d 17 for(int i=sizeof(x)<<3; i; i--)
robertbuc 0:56207c59a56d 18 putchar('0'+((x>>(i-1))&1));
robertbuc 0:56207c59a56d 19 }
robertbuc 0:56207c59a56d 20
robertbuc 0:56207c59a56d 21 void delbits();
robertbuc 0:56207c59a56d 22 void setbits();
robertbuc 0:56207c59a56d 23 void togbits();
robertbuc 0:56207c59a56d 24 void lst_3_5();
robertbuc 0:56207c59a56d 25
robertbuc 0:56207c59a56d 26 int main() {
robertbuc 0:56207c59a56d 27 //delbits();
robertbuc 0:56207c59a56d 28 //setbits();
robertbuc 0:56207c59a56d 29 //togbits();
robertbuc 0:56207c59a56d 30 //lst_3_5();
robertbuc 0:56207c59a56d 31 }
robertbuc 0:56207c59a56d 32
robertbuc 0:56207c59a56d 33 void lst_3_5()
robertbuc 0:56207c59a56d 34 {
robertbuc 0:56207c59a56d 35 //uint8_t value = 0b11111111; //löschen
robertbuc 0:56207c59a56d 36 //uint8_t mask = 0b00010100; //löschen
robertbuc 0:56207c59a56d 37
robertbuc 0:56207c59a56d 38 //uint8_t value = 0b11101011; //setzen
robertbuc 0:56207c59a56d 39 //uint8_t mask = 0b00010100; //setzen
robertbuc 0:56207c59a56d 40
robertbuc 0:56207c59a56d 41 uint8_t value = 0b10000100; //toggeln
robertbuc 0:56207c59a56d 42 uint8_t mask = 0b00010100; //toggeln
robertbuc 0:56207c59a56d 43
robertbuc 0:56207c59a56d 44
robertbuc 0:56207c59a56d 45
robertbuc 0:56207c59a56d 46 printf("value: ");
robertbuc 0:56207c59a56d 47 print_binary(value);
robertbuc 0:56207c59a56d 48 printf(" orginal! \n");
robertbuc 0:56207c59a56d 49
robertbuc 0:56207c59a56d 50 printf("\n===========================\n");
robertbuc 0:56207c59a56d 51 printf("mask: ");
robertbuc 0:56207c59a56d 52 print_binary(mask);
robertbuc 0:56207c59a56d 53 printf("\n===========================\n\n");
robertbuc 0:56207c59a56d 54
robertbuc 0:56207c59a56d 55 //value=value| (1 << 2) | (1 << 4); //1000 0000 wird zu 10010100 //setzen
robertbuc 0:56207c59a56d 56
robertbuc 0:56207c59a56d 57 //value=value&~mask; //LÖSCHEN an 3. und 5. Stelle
robertbuc 0:56207c59a56d 58 //value=value|mask; //SETZEN an 3. und 5. Stelle
robertbuc 0:56207c59a56d 59 //value=value^mask; //TOGGELN an 3. und 5. Stelle
robertbuc 0:56207c59a56d 60 printf("value: ");
robertbuc 0:56207c59a56d 61 print_binary(value);
robertbuc 0:56207c59a56d 62 printf(" geändert! \n");
robertbuc 0:56207c59a56d 63
robertbuc 0:56207c59a56d 64 }
robertbuc 0:56207c59a56d 65
robertbuc 0:56207c59a56d 66
robertbuc 0:56207c59a56d 67 void togbits()
robertbuc 0:56207c59a56d 68 {
robertbuc 0:56207c59a56d 69 uint32_t value = 0b10011001;
robertbuc 0:56207c59a56d 70 uint32_t mask = 0b11111111;
robertbuc 0:56207c59a56d 71
robertbuc 0:56207c59a56d 72 printf("value: ");
robertbuc 0:56207c59a56d 73 print_binary(value);
robertbuc 0:56207c59a56d 74
robertbuc 0:56207c59a56d 75 printf(" orginal! \n");
robertbuc 0:56207c59a56d 76
robertbuc 0:56207c59a56d 77
robertbuc 0:56207c59a56d 78 printf("\n===========================\n");
robertbuc 0:56207c59a56d 79 printf("mask: ");
robertbuc 0:56207c59a56d 80 print_binary(mask);
robertbuc 0:56207c59a56d 81 printf("\n===========================\n\n");
robertbuc 0:56207c59a56d 82
robertbuc 0:56207c59a56d 83 value=value^mask; //TOGGELN ^ EXOR und alles 1er in mask
robertbuc 0:56207c59a56d 84 printf("value: ");
robertbuc 0:56207c59a56d 85 print_binary(value);
robertbuc 0:56207c59a56d 86 printf(" geandert! \n");
robertbuc 0:56207c59a56d 87
robertbuc 0:56207c59a56d 88 }
robertbuc 0:56207c59a56d 89
robertbuc 0:56207c59a56d 90
robertbuc 0:56207c59a56d 91 void setbits()
robertbuc 0:56207c59a56d 92 {
robertbuc 0:56207c59a56d 93 uint16_t value = 0b00011000;
robertbuc 0:56207c59a56d 94 uint16_t mask = 0b10000001;
robertbuc 0:56207c59a56d 95
robertbuc 0:56207c59a56d 96 printf("value: ");
robertbuc 0:56207c59a56d 97 print_binary(value);
robertbuc 0:56207c59a56d 98
robertbuc 0:56207c59a56d 99 printf(" orginal! \n");
robertbuc 0:56207c59a56d 100
robertbuc 0:56207c59a56d 101
robertbuc 0:56207c59a56d 102
robertbuc 0:56207c59a56d 103 printf("\n===========================\n");
robertbuc 0:56207c59a56d 104 printf("mask: ");
robertbuc 0:56207c59a56d 105 print_binary(mask);
robertbuc 0:56207c59a56d 106 printf("\n===========================\n\n");
robertbuc 0:56207c59a56d 107
robertbuc 0:56207c59a56d 108 value=value|mask; //SETZEN
robertbuc 0:56207c59a56d 109 printf("value: ");
robertbuc 0:56207c59a56d 110 print_binary(value);
robertbuc 0:56207c59a56d 111 printf(" geandert! \n");
robertbuc 0:56207c59a56d 112 }
robertbuc 0:56207c59a56d 113
robertbuc 0:56207c59a56d 114
robertbuc 0:56207c59a56d 115 void delbits()
robertbuc 0:56207c59a56d 116 {
robertbuc 0:56207c59a56d 117
robertbuc 0:56207c59a56d 118 uint8_t value = 0b10011001;
robertbuc 0:56207c59a56d 119 uint8_t mask = 0b10000001;
robertbuc 0:56207c59a56d 120
robertbuc 0:56207c59a56d 121 printf("value: ");
robertbuc 0:56207c59a56d 122 print_binary(value);
robertbuc 0:56207c59a56d 123
robertbuc 0:56207c59a56d 124 printf(" orginal! \n");
robertbuc 0:56207c59a56d 125
robertbuc 0:56207c59a56d 126
robertbuc 0:56207c59a56d 127
robertbuc 0:56207c59a56d 128 printf("\n===========================\n");
robertbuc 0:56207c59a56d 129 printf("mask: ");
robertbuc 0:56207c59a56d 130 print_binary(mask);
robertbuc 0:56207c59a56d 131 printf("\n===========================\n\n");
robertbuc 0:56207c59a56d 132
robertbuc 0:56207c59a56d 133 value=value&~mask; //LÖSCHEN
robertbuc 0:56207c59a56d 134 printf("value: ");
robertbuc 0:56207c59a56d 135 print_binary(value);
robertbuc 0:56207c59a56d 136 printf(" geandert! \n");
robertbuc 0:56207c59a56d 137 }