1

I did a small test on bit shifting in C, and all of the shifts by 0, 8, 16 bits are OK and I understood what's happening.

But the 32 bits right or left shift which is not clear to me, the variable I'm doing the test with is 32-bit long.

Then, I changed the 32-bit variables which would hold the shifting results, but 32-bit right left shifts are the same!

Here's my code:

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

int main() {
    uint32_t code = 0xCDBAFFEE;

    uint64_t bit32R = code >> 32;
    uint16_t bit16R = code >> 16;
    uint8_t bit8R = code >> 8;
    uint8_t bit0R = code >> 0;

    uint64_t bit32L = code << 32;
    uint16_t bit16L = code << 16;
    uint8_t bit8L = code << 8;
    uint8_t bit0L = code << 0;

    printf("Right shift:\nbit32R %.16x\nbit16R %x\nbit8R %x\nbit0R %x\n\n",
           bit32R, bit16R, bit8R, bit0R);
    printf("Left shift:\nbit32L %.16x\nbit16L %x\nbit8L %x\nbit0L %x\n\n",
           bit32L, bit16L, bit8L, bit0L);
}

Here's the result I get:

Right shift:
bit32R 00000000cdbaffee
bit16R 0
bit8R cdba
bit0R ff

Left shift:
bit32L 00000000cdbaffee
bit16L 0
bit8L 0
bit0L 0

Process returned 61 (0x3D)   execution time : 0.041 s
Press any key to continue.
5
  • 2
    Your code invokes undefined behaviour. A shift value of 32 is too large for a 32 bit int (same for 16 bit if your platform has 16 bit int). And use the PRInX macros from inttypes.h to print fixed-width types. You have a missconception how integer operations work. Jan 29, 2017 at 11:07
  • I changed the include file to stdint! Which didn't change anything, but I forgot about stdint, and included inttypes. But I think they are the same, at least for my code.
    – R1S8K
    Jan 29, 2017 at 11:54
  • Maybebe before making assumptions, it is a good idea to read what the headers provide, how they are related and what PRInX means? Jan 29, 2017 at 11:58
  • Could you tell me in brief what inttypes and stdint offer?
    – R1S8K
    Jan 29, 2017 at 18:35
  • 1
    Is google down? Jan 29, 2017 at 20:16

3 Answers 3

6

Right shifting an integer by a number of bits equal or greater than its size is undefined behavior.

C11 6.5.7 Bitwise shift operators

Syntax

shift-expression: additive-expression
    shift-expression << additive-expression
    shift-expression >> additive-expression

Constraints

Each of the operands shall have integer type.

Semantics

The integer promotions are performed on each of the operands. The type of the result is that of the promoted left operand. If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.

The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are filled with zeros. If E1 has an unsigned type, the value of the result is E1 × 2E2, reduced modulo one more than the maximum value representable in the result type. If E1 has a signed type and nonnegative value, and E1 × 2E2 is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined.

The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type or if E1 has a signed type and a nonnegative value, the value of the result is the integral part of the quotient of E1 / 2E2. If E1 has a signed type and a negative value, the resulting value is implementation-defined.

The size of int on your platform seems to be at most 32 bits, so the initializers for bit32R and bit32L have undefined behavior.

The 64-bit expressions should be written:

uint64_t bit32R = (uint64_t)code >> 32;

and

uint64_t bit32L = (uint64_t)code << 32;

Furthermore, the formats used in printf are not correct for the arguments passed (unless int has 64 bits, which would produce different output).

Your compiler does not seem to be fully C99 compliant, you should add a final return 0; statement at the end of the body of function main().

Here is a corrected version:

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

int main(void) {
    uint32_t code = 0xCDBAFFEE;

    uint64_t bit32R = (uint64_t)code >> 32;
    uint16_t bit16R = code >> 16;
    uint8_t bit8R = code >> 8;
    uint8_t bit0R = code >> 0;

    uint64_t bit32L = (uint64_t)code << 32;
    uint16_t bit16L = code << 16;
    uint8_t bit8L = code << 8;
    uint8_t bit0L = code << 0;

    printf("Right shift:\n"
           "bit32R %.16"PRIx64"\n"
           "bit16R %"PRIx16"\n"
           "bit8R %"PRIx8"\n"
           "bit0R %"PRIx8"\n\n",
           bit32R, bit16R, bit8R, bit0R);

    printf("Left shift:\n"
           "bit32L %.16"PRIx64"\n"
           "bit16L %"PRIx16"\n"
           "bit8L %"PRIx8"\n"
           "bit0L %"PRIx8"\n\n",
           bit32L, bit16L, bit8L, bit0L);

    return 0;
}

The output is:

Right shift:
bit32R 0000000000000000
bit16R cdba
bit8R ff
bit0R ee

Left shift:
bit32L cdbaffee00000000
bit16L 0
bit8L 0
bit0L ee

this might not be what you expect, because the types of the variables are somewhat inconsistent.

11
  • return 0 for main() in C99 never was mandatory, it is said that if omitted return value of 0 would be expected provided by compiler (prototype of main() fuction is also platform dependent, void main(void) also acceptable). Tbh there very few fully C99-compliant compilers and even less C++11 ones at the moment, especially on embedded platforms. I wasn't even sure that he got access to PRIx macros, compiler I had at hand didn't had them in header Jan 29, 2017 at 16:33
  • @Swift: the status printed by the running environment Process returned 61 (0x3D) indicates the compiler did not add the implicit return 0; mandated by C99, hence my statement about the non-compliant compiler. I wouldn't be surprised if this environment is based on MSVC. In any case, it is good style to always return 0 (or some meaningful exit status) explicitly at the end of main(). The C99 implicit return semantics is a lame fix for sloppy code IMHO.
    – chqrlie
    Jan 29, 2017 at 18:44
  • Absolutely! That worked just perfectly. But why the (%.16x) didn't work for the 64 variable size? It shows the 64-bit hex size but the shifting is not correct. And with PRIx64, is working fine?
    – R1S8K
    Jan 29, 2017 at 18:55
  • @PerchEagle: "%.16"PRIx64 expects a value of type uint64_t and prints the hexadecimal conversion with at least 16 digits. If you use %.16x, you also get 16 hex digits (8 of which are necessarily 0), but the expected type is unsigned int which is probably incompatible with uint64_t, hence your printf format has undefined behavior. If type unsigned long long has 64 bits, PRIx16 could expand to "llu".
    – chqrlie
    Jan 29, 2017 at 19:00
  • But I get the same output without the type casting!
    – R1S8K
    Jan 29, 2017 at 20:25
1

One problem is that you are using %x to print a 64-bit integer. You should use the correct format specifier for each variable. There are macros for this available:

#define __STDC_FORMAT_MACROS
#include <inttypes.h>

// ...

printf("64 bit result: %" PRIx64 "\n", bit32R);
printf("16 bit result: %" PRIx16 "\n", bit16R);
printf("8 bit result: %" PRIx8 "\n", bit8R);

More information can be found here.

6
  • While %x is wrong, using PRIx64 doesn't change the result of the shift. Jan 29, 2017 at 11:20
  • If PRIx64 doesn't change the result, then why %x is wrong? I get the same results with your modification. I even can get the hex full format of 64-bit with %.16x modifier.
    – R1S8K
    Jan 29, 2017 at 12:00
  • @Perch these are platform-specific things. In general, it appears to work, but then either fails on other platforms, or fails when you write more advanced code. I'll admit that I don't bother to use all these macros when I'm writing code, but you should only skip them if you always know what platform you will be running on. Otherwise, disaster will occur.
    – user6754053
    Jan 29, 2017 at 12:28
  • I appreciate your hint, but anyway how do I suppose to know such macros when I want to write a code? Like this one "#define __STDC_FORMAT_MACROS"? Which I think is related to inttypes.h.
    – R1S8K
    Jan 29, 2017 at 18:58
  • 1
    @PerchEagle: The same way you are supposed to know to use uint64_t for 64-bit integers. But I understand that it is easy to see int*_t being used in some code, and to copy it, and that way it is hard to discover that you need the PRI* macros when trying to print them. It would be nice if the macros were mentioned in the manpage of printf()...
    – G. Sliepen
    Jan 29, 2017 at 19:40
0

You are not doing 64 bit left shift there, because code is uint32_t, so compiler uses 32bit version of operator. Also, you should tell print to use long long (same as uint64_t)

#include <cstdint>
#include <stdio.h>
int main ()
{
uint32_t code = 0xCDBAFFEE;


uint64_t bit32R=((uint64_t)code)>>32;
uint16_t bit16R=code>>16;
uint8_t bit8R=code>>8;
uint8_t bit0R=code>>0;

uint64_t bit32L=((uint64_t)code)<<32;
uint16_t bit16L=code<<16;
uint8_t bit8L=code<<8;
uint8_t bit0L=code<<0;


printf("Right shift:\nbit32R %llx\nbit16R %x\nbit8R %x\nbit0R %x\n\n", bit32R,bit16R,bit8R,bit0R);
printf("Leftt shift:\nbit32L %llx\nbit16L %x\nbit8L %x\nbit0L %x", bit32L,bit16L,bit8L,bit0L);
}

Result is:

Right shift:
bit32R 0
bit16R cdba
bit8R ff
bit0R ee

Leftt shift:
bit32L cdbaffee00000000
bit16L 0
bit8L 0
bit0L ee

You should use the macros defined in inttypes.h if you have C99 compliant compiler, sadly some platforms do not have those definitions. Format descriptors for printf are platform-dependent.

4
  • 1
    OP doesn't claim to do 64 bit. Also 64 bit isn't long long on all platforms. Jan 29, 2017 at 11:17
  • @Olaf Dietsche : that pretty much was the claim. uint64_t bit32L=code<<32; is same as uint32_t bit32L=code<<32. But I agree, though based on reaction of his printf output, his platform got 64bit as long long. Jan 29, 2017 at 11:20
  • But there is uint64_t which gives you 64-bit variable.
    – R1S8K
    Jan 29, 2017 at 18:59
  • @PerchEagle Yes, you get a 64 bit result, but only after producing undefined behaviour. If you want a reliable/defined 64 bit result, you must use 64 bit arguments for the calculation too. This is why Swift suggested a cast. Jan 29, 2017 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.