Question

I'm looking for an in-depth explanation on how each of these bitwise functions work. I think...

I'm looking for an in-depth explanation on how each of these bitwise functions work. I think I understand the underlying concepts of each, but I want to be sure. I'm looking for a piece by piece break down of each function.

Function 1

int logicalShift(int x, int n) {
x >>= n;

return x & ~(~(~0 << n) << (32 + ~n + 1));

Function 2

{ int bang(int x) {
int neg = ~x + 1;
return ((~(x | neg)) >> 31) & 0x1;
}

Function 3

int bitCount(int x) {
int mask1 = 0x55 + (0x55 << 8);
int mask2 = 0x33 + (0x33 << 8);
int mask3 = 0x0f + (0x0f << 8);
int mask4 = 0xff + (0xff << 16);
int mask5 = 0xff + (0xff << 8);
mask1 += mask1 << 16;
mask2 += mask2 << 16;
mask3 += mask3 << 16;
x = (x & mask1) + ((x >> 1) & mask1);
x = (x & mask2) + ((x >> 2) & mask2);
x = (x & mask3) + ((x >> 4) & mask3);
x = (x & mask4) + ((x >> 8) & mask4);
x = (x & mask5) + ((x >> 16) & mask5);

return x;

Homework Answers

Answer #1

Function 1:
In this function logical shift in which we shift var x to the right by n moves. We can look at the limit that n is greater equal to 1 and lesser equal to 31.

Function 2:   

In the bang function the bit which is on front ((~x) | x) is always 1 for every number but not for 0
For 0 for the sign bit it doesn't matter it is positive or negative but for others numbers it matter.

Function 3:

The purpose of this function is to return count of number of 1's in word
We are using 5 integer type mask variables in which we are adding memory location and shifting the address 8 bits to the left after shifting all 5.
Now we are shifting 16bits to the left in mask1 mask2 mask3 by adding their individual previous mask value.
Now we are finding value of x and shifting result to the right as the binary power of 2

IF YOU HAVE ANY QUERY PLEASE COMMENT DOWN BELOW
PLEASE GIVE A THUMBS UP

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
PLEASE ANSWER I WILL RATE YOUR ANSWER AND THUMBS UP For the following C functions: int...
PLEASE ANSWER I WILL RATE YOUR ANSWER AND THUMBS UP For the following C functions: int q3(int x) {     return ((~x)>>31) & 0x1; } int q4(int x) {     int mask = x>>31;     int y= (x ^ mask);     int z = (~mask + 1);     return (y+z); } int q5(int x){     int y = !x;     int z = x >> 31;     z = z | y;     return !z; } int q6(int x) {...
Evaluating if a value is negative using bitwise operators int test_dl3(int x) {     int i;...
Evaluating if a value is negative using bitwise operators int test_dl3(int x) {     int i;     for (i = 0; i < 32; i+=2)        if ((x & (1<          return 0;            return 1; } Legal ops: ! ~ & ^ | + << >> Max ops: 12 I have a few questions similar to this one, but I'm running into much the same problem for all of them. The behavior of this code appears to be that...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions are suggested for easier procedure of making function.) void search_node(struct linked_list* list, int find_node_ value) (The function to make) This function finds the node from the list that value is same with find_node_value and count the order of the node. This function should print message “The order of (node_value) is (order).” and error message “Function search_node : There is no such node to search.”....
I have a few questions that I'm not sure how to go about doing? This is...
I have a few questions that I'm not sure how to go about doing? This is what I was asked: Use the table of transforms, linearity, and s-shifting to find F(s) = L (f(t)) for each of the following functions. Write the result as a rational function P(s)/Q(s). 1) f(t) = −4 + e^(−t) 2) f(t) = e^(4t) − 10 + sin t 3) f(t) = cos(2t − 5) (Hint: use the identity cos x ± y = cos x...
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an...
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an executable with gcc -Wall -DUNIT_TESTS=1 array-utils5A.c The definitions for is_reverse_sorted and all_different are both defective. Rewrite the definitions so that they are correct. The definition for is_alternating is missing. Write a correct definition for that function, and add unit tests for it, using the unit tests for is_reverse_sorted and all_different as models. Please explain the logic errors present in in the definition of is_reverse_sorted...
in C++ Please and thanks Here is a list of 6 numbers. Use the selection sort...
in C++ Please and thanks Here is a list of 6 numbers. Use the selection sort algorithm to sort this list. Fill in this table with each iteration of the loop in the selection sort algorithm. Mark the place from which you are looking for the 'next smallest element'. In this display, the upper numbers are the indices, the lower numbers are in the corresponding positions. Use the several rows provided to show the sequence of steps. 0 1 2...
For some reason I followed the steps in my project and I am getting the incorrect...
For some reason I followed the steps in my project and I am getting the incorrect output and when I am submitting it, it gives me compilation error. Printing empty array -- next line should be blank Testing append: Shouldn't crash! Should print 100 through 110 below, with 110 on a new line: 100 101 102 103 104 105 106 107 108 109 110 Checking capacity of new array: OK Append test #2: Should print 100 through 120 below, on...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the following subsections can all go in one big file called pointerpractice.cpp. 1.1     Basics Write a function, int square 1(int∗ p), that takes a pointer to an int and returns the square of the int that it points to. Write a function, void square 2(int∗ p), that takes a pointer to an int and replaces that int (the one pointed to by p) with its...
​​​​​​ Below are ten functions. Find the first derivative of each. All of the derivatives can...
​​​​​​ Below are ten functions. Find the first derivative of each. All of the derivatives can be found by using combinations of the constant rule, power function rule and sum-difference rule.   Do not use the product rule. It is not needed. The degree of difficulty (more or less) increases from (a) to (j). Be sure to show intermediate work. Five points are awarded for correctly developed derivatives. There is no partial credit, because an incorrect derivative is useless. For example,...
write a code in python Write the following functions below based on their comments. Note Pass...
write a code in python Write the following functions below based on their comments. Note Pass is a key word you can use to have a function the does not do anything. You are only allowed to use what was discussed in the lectures, labs and assignments, and there is no need to import any libraries. #!/usr/bin/python3 #(1 Mark) This function will take in a string of digits and check to see if all the digits in the string are...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT