in Education by
Given an integer typedef: typedef unsigned int TYPE; or typedef unsigned long TYPE; I have the following code to reverse the bits of an integer: TYPE max_bit= (TYPE)-1; void reverse_int_setup() { TYPE bits= (TYPE)max_bit; while (bits <<= 1) max_bit= bits; } TYPE reverse_int(TYPE arg) { TYPE bit_setter= 1, bit_tester= max_bit, result= 0; for (result= 0; bit_tester; bit_tester>>= 1, bit_setter<<= 1) if (arg & bit_tester) result|= bit_setter; return result; } One just needs first to run reverse_int_setup(), which stores an integer with the highest bit turned on, then any call to reverse_int(arg) returns arg with its bits reversed (to be used as a key to a binary tree, taken from an increasing counter, but that's more or less irrelevant). Is there a platform-agnostic way to have in compile-time the correct value for max_int after the call to reverse_int_setup(); Otherwise, is there an algorithm you consider better/leaner than the one I have for reverse_int()? Thanks. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
#include #include #define TYPE_BITS sizeof(TYPE)*CHAR_BIT typedef unsigned long TYPE; TYPE reverser(TYPE n) { TYPE nrev = 0, i, bit1, bit2; int count; for(i = 0; i < TYPE_BITS; i += 2) { /*In each iteration, we swap one bit on the 'right half' of the number with another on the left half*/ count = TYPE_BITS - i - 1; /*this is used to find how many positions to the left (and right) we gotta move the bits in this iteration*/ bit1 = n & (1<<(i/2)); /*Extract 'right half' bit*/ bit1 <<= count; /*Shift it to where it belongs*/ bit2 = n & 1<<((i/2) + count); /*Find the 'left half' bit*/ bit2 >>= count; /*Place that bit in bit1's original position*/ nrev |= bit1; /*Now add the bits to the reversal result*/ nrev |= bit2; } return nrev; } int main() { TYPE n = 6; printf("%lu", reverser(n)); return 0; } This time I've used the 'number of bits' idea from TK, but made it somewhat more portable by not assuming a byte contains 8 bits and instead using the CHAR_BIT macro. The code is more efficient now (with the inner for loop removed). I hope the code is also slightly less cryptic this time. :) The need for using count is that the number of positions by which we have to shift a bit varies in each iteration - we have to move the rightmost bit by 31 positions (assuming 32 bit number), the second rightmost bit by 29 positions and so on. Hence count must decrease with each iteration as i increases. Hope that bit of info proves helpful in understanding the code...

Related questions

0 votes
    Class brush{ private: integer size, rcode function getdata() { }// Statement 1 public: integer name // Statement ... //Statement 4 } Select the correct answer from above options...
asked Dec 21, 2021 in Education by JackTerrance
0 votes
    In virtual memory system size of virtual address is ,32 bit, size of physical address is 30 bit, page size ... each page table entry. Select the correct answer from above options...
asked Dec 24, 2021 in Education by JackTerrance
0 votes
    Whats the lowest number can a computer with word size of 3 bit represent in ones complement? Select the correct answer from above options...
asked Nov 30, 2021 in Education by JackTerrance
0 votes
    When I send unsupported JSON request I receive stack trace from Jackson that it doesn't have this property. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    Write a C program to accept an integer number between 1 and 9. Write the value of the number in words. Select the correct answer from above options...
asked Dec 14, 2021 in Education by JackTerrance
0 votes
    write a program to read an integer and find its reverse. note: if user enter 1234 then the output should be 4321 in Python Select the correct answer from above options...
asked Dec 2, 2021 in Education by JackTerrance
0 votes
    In case of integer overflow, Which of the following option/s is/are true? 1.It is a result of an attempt to ... 4.None of the above Select the correct answer from above options...
asked Nov 30, 2021 in Education by JackTerrance
0 votes
    write an algorithm to accept an integer number and print the factors of it Select the correct answer from above options...
asked Nov 27, 2021 in Education by JackTerrance
0 votes
0 votes
    Is there a data structure that implements the Map interface and holds an array of elements with continuous ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 24, 2022 in Education by JackTerrance
0 votes
    I have some columns that have the same names. I would like to add a 1 to the repeating column ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 6, 2022 in Education by JackTerrance
0 votes
    How would you write (in C/C++) a macro which tests if an integer type (given as a parameter) is ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 25, 2022 in Education by JackTerrance
...