Bit shifting and bitwise operators

In summary, the program is a function in C++ that takes in an unsigned integer and performs various logical shifts and operations to compute a value, which is then returned as the sum of all the operations. The function is best understood when the input and output are expressed in hexadecimal format instead of decimal.
  • #1
fsbadr
18
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
  • #2
You will see what it is doing much easier if you express the input and output in hexadecimal rather than decimal format.
 
  • #3


I can provide some insight into what this function is doing. This function is using bitwise operators, specifically bit shifting, to manipulate the bits of the input number n. Bit shifting is a way to move the bits of a number to the left or right, effectively multiplying or dividing the number by a power of 2. In this case, the function is using logical right shifts (>>), which shifts the bits to the right, and logical left shifts (<<), which shifts the bits to the left.

In the first line, the function is performing a logical right shift with a value of 24. This means that it is shifting the bits of n to the right by 24 places, effectively dividing n by 2^24. In this case, n is 1200, so the result of this operation is 0.

In the second line, the function is performing a logical left shift with a value of 8, and then performing a logical and operation with the hex value 0x00FF0000. This means that it is shifting the bits of n to the left by 8 places, and then only keeping the bits that align with the 0x00FF0000 value. In this case, the result is 262144.

In the third line, the function is performing a logical right shift with a value of 8, and then performing a logical and operation with the hex value 0x0000FF00. This means that it is shifting the bits of n to the right by 8 places, and then only keeping the bits that align with the 0x0000FF00 value. In this case, the result is 0.

Finally, in the last line, the function is performing a logical left shift with a value of 24, effectively multiplying n by 2^24. The result of this operation is 2952790016.

The temp variable is the sum of all these operations, which in this case is 2952790016. This value is then returned by the function.

In summary, this function is using bitwise operators to manipulate the bits of the input number n in order to perform mathematical operations such as division and multiplication. The specific values used in the operations may vary depending on the input number n and the desired result.
 

What are bit shifting and bitwise operators?

Bit shifting and bitwise operators are used in computer programming to manipulate binary numbers at the bit level. Bit shifting involves moving the bits of a number to the left or right, while bitwise operators perform logical operations on individual bits.

What is the purpose of using bit shifting and bitwise operators?

The main purpose of using bit shifting and bitwise operators is to optimize performance and memory usage in computer programming. They are commonly used in low-level programming languages to perform calculations and manipulate data at a more efficient level.

What are some common bitwise operators?

Some common bitwise operators include AND (&), OR (|), XOR (^), NOT (~), and SHIFT (<< or >>). These operators allow for logical operations such as comparing, combining, and manipulating individual bits in a binary number.

What is the difference between left and right bit shifting?

Left bit shifting involves moving the bits of a number to the left, which is equivalent to multiplying the number by 2 for each shift. Right bit shifting, on the other hand, involves moving the bits to the right, which is equivalent to dividing the number by 2 for each shift.

How are bit shifting and bitwise operators used in real-world applications?

Bit shifting and bitwise operators are commonly used in low-level programming for tasks such as data compression, encryption, and error correction. They are also used in graphics and audio processing, network protocols, and other applications that require efficient manipulation of binary data.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
29
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Back
Top