c - Exchange the bytes -
I want to exchange a number of bytes. Example example is the binary representation of a number Can you please help? The current code is below: If you want to reverse only 32-bit numbers, The transfer technology, which separates each byte field by using bit-masks and logical You will now end up with the last value in Update: OK, I'm looking at you code, and I'm focusing on the following: 00000001 00011000 00000100 00001110 . I want to reverse it:
00001110 00000100 00011000 00000001 .
Bit of intent (int n) {int i, k, and mask; For (i = 15; i> = 0; i--) {and mask = 1 & lt; & Lt; I; = N & amp; k; AndMask; K == 0? (Court's & lt; "0"): (Court's & lt; <"1"); }} Reverse (int a) {int b = a & lt; & Lt; 8; Int c = a & gt; & Gt; 8; Return (bc); } Int main () {int a = 10; ShowBits (a); Int b = reverse (a); ShowBits (b); Cin.get (); }
and , and then swaps those bytes by the appropriate number of transferred bits using bit-shift operators < Code> & gt; & Gt; and
& lt; & Lt; Then you can rearrange bits like logical
or :
int temp = 0x12345678; Temp = ((0xFF and Temporary) <24 ((0xFF00 and floating) & lt; <8) | ((0xFF0000 and floating) & gt; & gt; 8). ((0xFF000000 and floating)>> 24) ;;
temporary in
0x78563412 .
int < / Code> Size It seems that you want 32-bit working with the binary number you post ... so your
showBits function to display the entire 32-bit integer There is no bicycle through enough bits, as far as I can see, it will only appear up to 16 short bits. So you have to clarify whether you are working on platforms that define
int as 16 or 32-bit.
reverse function is not correct. If you have 32-bit
int like
0x12345678 , and you have left it with 8-bit, then you end up with
0x34567800 Will go Similarly, when you move it correctly with 8-bits, you will simply end up with
0x00123456 . The values are not rotated through positions related to transferring a number (i.e., a shift to the left of the 8-bit will not give you
0x34567812 ). Apart from this, even if this is done, logical values of rotated values will still not be reversed by the values, instead, you can use bit-mask and logical
AND Using the above technique should use the , and then bits 32-bit words to move those bits to the appropriate number.
Comments
Post a Comment