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: Implicit declaration of function ‘read’ and ‘write’ 6

While working on an socket-based application, we received the following warnings from the compiler:

implicit declaration of function 'read'
implicit declaration of function 'write'

read and write functions are declared in unistd.h which we forgot to include in our code.

Adding the directive


#include <unistd.h>

to the source file that used read and/or write removed the warnings.


How does the expression, “*pointer++” evaluate?

*pointer++ will increment the position of the pointer first but it will return the value that was pointed before the position of the pointer changed.

*pointer++ is equivalent to *(pointer++). This happens because the postfix ++ and -- operators have higher precedence than the indirection (dereference).
You can read more about the precedence order at this very helpful article at Wikipedia: https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence

To increment the value pointed to by pointer, use (*pointer)++.

To increment the position of the pointer and return the value that is pointed after the position of the pointer changed use *++pointer.

Examples

The following example will increment the position of the pointer but return the value that was originally pointed.
By the end of the following block, pointer will point to position 1 and the value variable will have the value 11.


const unsigned int values[] = {11, 12, 14, 18};
const unsigned int *pointer = values;
const unsigned int value = *pointer++;

The following example will increment the position of the pointer and return the value that the new position is pointing to.
By the end of the following block, pointer will point to position 1 and the value variable will have the value 12.


const unsigned int values[] = {11, 12, 14, 18};
const unsigned int *pointer = values;
const unsigned int value = *++pointer;