I'm trying to show an unsigned byte from a function. The byte that I return in the function is different from the value i see when showing it in the CLI. I use the following code:
#include <iostream>
#include <bitset>
typedef unsigned char Byte;
typedef unsigned int Uint;
Byte makeMask(Uint pos) {
switch (pos) {
case 0:
return 10000000;
case 1:
return 01000000;
case 2:
return 00100000;
case 3:
return 00010000;
case 4:
return 00001000;
case 5:
return 00000100;
case 6:
return 00000010;
case 7:
return 00000001;
}
}
int main() {
//Variabelen aanmaken
Uint pos;
Byte source;
//Om een int tussen 0 en 7 blijven vragen totdat deze is ingevoerd
do {
std::cout << "Vul een getal tussen de 0 en de 7 in: ";
std::cin >> pos;
} while (pos < 0 || pos > 7);
//Om een source mask vragen
std::cout << "Vul een source in: ";
std::cin >> source;
//Functies callen en printen
Byte magic = makeMask(pos);
std::bitset < 8 > meme(magic);
std::cout << "Getrokken mask: " << meme << std::endl;
std::cout << "Getrokken mask: " << makeMask(pos) << std::endl;
return 0;
}
My output is as followed:
kevin@ubuntu:~/C++_Opdrachten/Week1/Week1_Op7$ ./a.out
Vul een getal tussen de 0 en de 7 in: 6
Vul een source in: 11110000
Getrokken mask: 00001000
Getrokken mask:
Why is the bit in the first method on the wrong place and why don't I see any output when using the second method?