C++


C/C++: Change position of bytes 1 and 2 with bytes 3 and 4 in a 32bit unsigned integer

The following function will produce a new 32bit value where bytes 1 and 2 were moved in place of bytes 3 and 4 and vice versa.

[download id=”3642″]

#include <stdio.h>
#include <stdlib.h>

const unsigned int move_bytes_1_2_after_4 (const unsigned int input) {
  //We get the two leftmost bytes and move them to the positions of the two rightmost bytes.
  const unsigned int first_two_bytes = (input >> 16) & 0x0000FFFF;
  //We get the two rightmost bytes and move them to the positions of the two leftmost bytes.
  const unsigned int last_two_bytes = (input << 16) & 0xFFFF0000;
  //We combine the two temporary values together to produce the new 32bit value where bytes 1 and 2 were moved in place of bytes 3 and 4 and vice versa.
  return (first_two_bytes | last_two_bytes);
}

int main(void) {
  const unsigned int value = 0xABCD0123;
  printf ("Original: 0x%08x\n", value);
  const unsigned int modified = move_bytes_1_2_after_4(value);
  printf ("Modified: 0x%08x\n", modified);
  return EXIT_SUCCESS;
}

Executing the above code will produce the following output:

Original: 0xabcd0123
Modified: 0x0123abcd

[download id=”3642″]


C++: How to print an unsigned character (unsigned byte – uint8_t) using cout 1

When you try to print an unsigned 8-bit (1 byteuint8_t) integer through cout, you will notice that instead of getting the arithmetic value of the variable on the output, you will get its character representation of the ASCII table. This issue occurs due to the fact that there is no data type for unsigned 8-bit integer in C++ and the uint8_t is nothing more than a typedef of the unsigned char data type. When cout tries to print the uint8_t it will call the ostream& operator<< (ostream& os, unsigned char c); which will insert the character representation of the character variable c to the os.

Below we propose a few methods to resolve this issue.

Method A: Convert the variable to an unsigned int before printing it

The following example will convert the value of the variable to an unsigned int before printing it so that cout will call the ostream& operator<< (unsigned int val); and it will print it as a number.


//For unsigned characters
cout << unsigned(c) << endl;
//For signed characters
cout << int(c) << endl;

Method B: Static cast the variable to an unsigned int before printing it

The following example will use static_cast to cast the value of the variable to an unsigned int before printing it so that cout will call the ostream& operator<< (unsigned int val); and it will print it as a number.


//For unsigned characters
cout << static_cast<unsigned int>(c) << endl;
//For signed characters
cout << static_cast<int>(c) << endl;

Method C: Cast the variable to an unsigned int before printing it

The following example will cast the value of the variable to an unsigned int before printing it so that cout will call the ostream& operator<< (unsigned int val); and it will print it as a number.


//For unsigned characters
cout << (unsigned int) c << endl;
//For signed characters
cout << (int) c << endl;

Method D: Add a unary + operator before the variable to create an arithmetic operation that does not affect the value and print its result

The following example will add a unary + operator before the variable so that it will produce an arithmetic result and print that one so that cout will treat the result as a number.


cout << +c << endl;

Method E: Use Argument-dependent name lookup (ADL)

Argument-dependent name lookup, applies to the lookup of an unqualified function name depending on the types of the arguments given to the function call.
Reference: http://en.wikipedia.org/wiki/Argument-dependent_name_lookup

In the following example we change the behavior of cout using a custom namespace to achieve the goal of printing and char as an 8-bit integer.
Specifically, we overload the inline std::ostream &operator<<(std::ostream &os, const char c); for cases where we the variable is not defined to be signed or not, to check if the input char variable is signed or unsigned and then perform a static_cast to the proper type.
Also, inline std::ostream &operator<<(std::ostream &os, const signed char c); and inline std::ostream &operator<<(std::ostream &os, const unsigned char c); are also overloaded to perform the correct static_cast immediately when the the type of the variable is known.


#include <iostream>

namespace bytes {
    inline std::ostream &operator<<(std::ostream &os, const char c) {
        return os << (std::is_signed<char>::value
                ? static_cast<int>(c)
                : static_cast<unsigned int>(c));
    }

    inline std::ostream &operator<<(std::ostream &os, const signed char c) {
        return os << static_cast<int>(c);
    }

    inline std::ostream &operator<<(std::ostream &os, const unsigned char c) {
        return os << static_cast<unsigned int>(c);
    }
}

using namespace std;

int main() {

    const uint8_t c = 64;

    {
        using namespace bytes;
        cout << c << endl;
    }
    {
        cout << c << endl;
    }

    return 0;
}


C++ How to make cout not use scientific notation

To force cout to print numbers exactly as they are and prevent it from using the scientific notation, we can use the std::fixed I/O manipulator as follows

#include <iostream>

using namespace std;

int main()
{
    std::cout << "The number 0.0001 in fixed:      " << std::fixed << 0.0001 << endl
              << "The number 0.0001 in default:    " << std::defaultfloat << 0.0001 << endl;

    std::cout << "The number 1000000000.0 in fixed:      " << std::fixed << 1000000000.0 << endl
              << "The number 1000000000.0 in default:    " << std::defaultfloat << 1000000000.0 << endl;
return 0;
}

Output

The number 0.0001 in fixed:      0.000100
The number 0.0001 in default:    0.0001
The number 1000000000.0 in fixed:      1000000000.000000
The number 1000000000.0 in default:    1e+09