![]() In this algorithm, we need to calculate the index of the array (look-up table) four times to getting the appropriate value from the look-up table. In this method, we are performing the AND operation of data with 0xFF to calculate the index of the array. In which we create a table of the hex value from 0 to 255. This is the simplest method to reverse the bits of an integer. Tmp |= 1 << ((INT_BITS - 1) - iLoop) //putting the set bits of num in tmp If((num & (1 << iLoop))) // check set bits of num Unsigned int tmp = 0 // Assign num to the tmp Unsigned int ReverseTheBits(unsigned int num) (n & (1 >= 1 // shift num because LSB already assigned to tmp #define INT_BITS ( sizeof(int) * CHAR_BITS) When num becomes zero then left shift tmp 12 times to get the exact reverse number 11000000000000. ![]() First, we assign the num value to the tmp and get the LSB of num.Īfter that, we iterate a loop until the num becomes zero with putting set bits in tmp. Let assume here is a number num (short int) which contains a value 0000000000001100. When num becomes zero then shift the remaining bits of temp through the count. This is a simple method, we take an integer tmp and putting set bits of the num in tmp until the num becomes zero. } In below section, I am describing 5 ways to reverse bits of an integer. ![]() Use to print the data in binary formatįor (iPos = (INT_BITS -1) iPos >= 0 iPos-) #define INT_BITS ( sizeof(int) * CHAR_BITS) //bits in integer ![]() Example code to print the data in binary format. So here, I am creating a simple program to print the number in binary format. Note: In the c language, printf lacks the ability to print the data in binary format. Use to shift a sequence of Bit toward Right There is some important list of the bitwise operator. So it is my personal advice before looking the example code read the unary operators and try this yourself first.īefore going to example code I am discussing bit-wise operator which are frequently used in below example code. If you have good knowledge of unary operators then bit reversal is a very simple question for you otherwise, it can be difficult. ![]() There are several methods to reverse the bits of an integer. In the interview generally, bit reversal is the common question for the interviewer. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |