Bit in c

how can you rotate and rotate hexadecimal numbers and return a number in C with bitwise operators?

eg:

0xabcd -> 0xdcba 0xabcd -> 0xdabc 
+4
source share
4 answers

I don’t know where to start this question. Plus I feel the homework.

Some moments:

  • There is no such thing as a hexadecimal number. Hex is just a designation. How do you rotate and rotate decimal numbers and return a number in C? For instance:

    1776 β†’ 6771

    1776 β†’ 6771?

  • To solve this problem, you need a deep understanding of positional notation, be it base 10, base 16, base 2, or whatever you have.

  • All you need is addition, subtraction, multiplication and division. These are operations with numbers. The module is also very useful.

  • If you want to multiply or divide by two forces, I recommend you the left shift operators << and right shift >> . They work great for numbers that are represented using C types of unsigned or unsigned long .

+13
source

Hexadecimal numbers are numbers, as the Norman answer indicates. However, 1 hexadecimal digit = 4 bits, so these operations really make sense as things you might want to do with integer values.

The second is 4-bit rotation. See Recommendations on cyclic shift (rotary) operations in C ++ for best practices for compiler-friendly rotations that protect against C / C ++ undefined -behaviour.

If your input is not 8, 16, 32, or 64 bits, then you may need to shift + the mask manually, rather than relying on a shift in zeros.


The first will require more code: it changes the order of nibbles. For this, there is no machine instruction or an easy way to build it from several bitwise operations on the whole quantity at once.

I think you will have to reverse the byte order, and then change the nibble order in each byte (8-bit rotation by 4).

+1
source

For pleasure, it follows in a recursive solution that works for any width of digits.

 #include <limits.h> unsigned ReverseHex(unsigned x, unsigned DigitWidth) { if (DigitWidth <= 1) { return x; } unsigned SideDigitWidth = DigitWidth / 2; unsigned SideBitWidth = SideDigitWidth * 4; unsigned CenterAndRightDigitWidth = DigitWidth - SideDigitWidth; unsigned CenterAndRightBitWidth = CenterAndRightDigitWidth * 4; unsigned CenterAndRight = x & ((1u << CenterAndRightBitWidth) - 1); unsigned Right = x & ((1u << SideBitWidth) - 1); unsigned Center = CenterAndRight - Right; return ReverseHex(x >> CenterAndRightBitWidth, SideDigitWidth) + Center + (ReverseHex(Right, SideDigitWidth) << CenterAndRightBitWidth); } int main(void) { printf("%X\n", ReverseHex(0x1234, 4)); printf("%X\n", ReverseHex(0x12345, 5)); printf("%X\n", ReverseHex(0x1234567, 7)); printf("%X\n", ReverseHex(0x12345678, 8)); return 0; } 

Exit

 4321 54321 7654321 87654321 
0
source

To change the number using bitwise operations:

Perform bitwise AND operation using the source number with the appropriate mask to extract the sixth digit (4 bits) from the source number.

Move this extracted bit pattern to a new location.

Bitwise OR modified bit patterns together.

Hope this helps.

-one
source

All Articles