1

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?

Woask
  • 59
  • 1
  • 9
  • `10000000` is an integer literal whose value does not fit into a `char` – 463035818_is_not_an_ai Jun 07 '18 at 18:17
  • the others also dont quite fit into a byte, see here: https://stackoverflow.com/questions/26568200/what-is-special-about-numbers-starting-with-zero – 463035818_is_not_an_ai Jun 07 '18 at 18:19
  • Most of those values are too large to fit in an 8-bit `unsigned char`, so the value returned by `makeMask` for the first five cases will be 0. If the compiler defines `unsigned char` to have more than 8 bits some of those will be valid, but that's unusual. – Pete Becker Jun 07 '18 at 18:19
  • 2
    The value 00001000 is an *octal* value, not binary or hex or decimal. – Thomas Matthews Jun 07 '18 at 18:19
  • I recommend changing your numbers to hexadecimal notation. Otherwise you'll have to represent binary as a text string; and that becomes a lot of work (converting between string and number). – Thomas Matthews Jun 07 '18 at 18:20
  • 2
    `typedef unsigned char Byte;` - [std::byte](https://en.cppreference.com/w/cpp/types/byte) *is* a thing, you know. No need for shenanigans and custom typedefs. – Jesper Juhl Jun 07 '18 at 18:22
  • 1
    Use a `0b` prefix if you want to specify binary numbers (since C++14). – Jesper Juhl Jun 07 '18 at 18:28

1 Answers1

1

You may want to simplify your makeMask to use bit shifting, instead of switch:

uint8_t makeMask(size_t bit_position)
{
  return 0x80 >> bit_position;
}

There are many methods for displaying numbers in binary. Search the internet for "c++ binary to text".

Edit 1:
You could make the function inline or replace the function call with the single statement. This would be more efficient than calling a one line function.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • "You could make the function inline ... This would be more efficient than calling a one line function" - `inline` is nothing more than a compiler *hint* (which most ignore anyway), and the compiler will most likely inline the one-line function *anyway* and even if it does not, it will hardly ever matter in real life, except for *very* hot paths/loops. Just keep it simple and readable. Trust the optimizer. And profile if you have an *actual* performance problem. – Jesper Juhl Jun 07 '18 at 18:34