Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#FFcccc
langc
void func(int si_a;
, int si_b;
int sresult;

void func(void) {
  /* Initialize si_a and si_b */
  int sresult = si_a << si_b;

  /* ... */
}

...

Code Block
bgColor#FFcccc
langc
void func(unsigned int ui_a;
, unsigned int ui_b;
unsigned int uresult;

void func(void) {
  /*unsigned Initialize ui_a and ui_b */
  int uresult = ui_a << ui_b;

  /* ... */
}

...

Code Block
bgColor#ccccff
langc
#include <limits.h>
 
void func(unsigned int ui_a;
, unsigned int ui_b;
unsigned int uresult;

void func(void) {
  /*unsigned Initializeint ui_auresult and ui_b */= 0;
  if (ui_b >= UWIDTH( unsigned int, UINT_MAX)) {
    /* Handle error condition */
  } else {
    uresult = ui_a << ui_b;
  }

  /* ... */
}

...

Code Block
bgColor#FFcccc
langc
void func(unsigned int ui_a;
, unsigned int ui_b;
unsigned int uresult;

void func(void) {
  /*unsigned Initialize ui_a and ui_b */
  int uresult = ui_a >> ui_b;

  /* ... */
}

...

Code Block
bgColor#ccccff
langc
#include <limits.h>
 
void func(unsigned int ui_a;
, unsigned int ui_b;
unsigned int uresult;

void func(void) {
  /*unsigned Initializeint ui_auresult and ui_b */= 0;
  if (ui_b >= UWIDTH( unsigned int, UINT_MAX)) {
    /* Handle error condition */
  } else {
    uresult = ui_a >> ui_b;
  }
  /* ... */
}

...

The sa[rl]l instructions take a bit mask of the least significant 5 bits from %cl to produce a value in the range [0, 31] and then shift %eax that many bits:

Code Block
// 64 -bit shifts on IA-32 platforms become
sh[rl]dl  %eax, %edx
sa[rl]l   %cl, %eax

...

...

[Dowd 2006]Chapter 6, "C Language Issues"
[C99 Rationale 2003]Subclause 6.5.7, "Bitwise Shift Operators"
[Seacord 2013]Chapter 5, "Integer Security"
[Viega 2005]Section 5.2.7, "Integer Overflow"

...

 

...