Bit shifting and bitwise operators

  • Thread starter Thread starter fsbadr
  • Start date Start date
  • Tags Tags
    Bit Operators
Click For Summary
SUMMARY

The discussion focuses on a C++ function that utilizes bit shifting and bitwise operators to manipulate an unsigned integer. The function, myfunc, takes an input of 1200 and performs a series of logical shifts and bitwise operations, resulting in a final output of 2953052160. Key operations include right shifts, left shifts, and masking with hexadecimal values, which collectively transform the input into a new unsigned integer. Understanding these operations is crucial for grasping how bit manipulation can be applied in programming.

PREREQUISITES
  • C++ programming language proficiency
  • Understanding of unsigned integers
  • Knowledge of bitwise operators and logical shifts
  • Familiarity with hexadecimal notation
NEXT STEPS
  • Study C++ bitwise operators in detail
  • Learn about logical shifts and their applications in programming
  • Explore hexadecimal representation of integers
  • Investigate performance implications of bit manipulation in C++
USEFUL FOR

Software developers, particularly those working with low-level programming and optimization, as well as students studying computer science concepts related to bit manipulation and data representation.

fsbadr
Messages
16
Reaction score
0

Homework Statement


folks,

I have a small problem understanding a function as to what its doing:

I have run this program in C++. I will comment the lines of code as per my understanding. Your insight would be useful

unsigned int myfunc(unsigned int n)
{
// for n here I took 1200

unsigned int temp =
//logical shift done here as it is unsigned, division for 1200 came to 0 as it was rounded
(n >> 24) |

// logical shift as well, logical and operation with hex value, result is 262144
((n << 8) & 0x00FF0000) |

// logical shift as well, logical and operation with hex value, came to 0 as it is rounded
((n >> 8) & 0x0000FF00) |

// computes to 2952790016
(n << 24);

//temp is then the sum of all operations which in this case is 0 + 262144 + 0 + 2952790016 totaling 2953052160.
return temp;

the value returned is the sum of all operations.

}

thanks for your help.

Homework Equations



This is a real world scenario function, what is it doing?

The Attempt at a Solution



In part (a), I have put in my findings at attempting the solution.
 
Physics news on Phys.org
You will see what it is doing much easier if you express the input and output in hexadecimal rather than decimal format.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
4K
Replies
29
Views
5K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 16 ·
Replies
16
Views
3K
Replies
1
Views
4K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 14 ·
Replies
14
Views
5K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K