c - Exchange the bytes -


I want to exchange a number of bytes. Example example is the binary representation of a number 00000001 00011000 00000100 00001110 . I want to reverse it: 00001110 00000100 00011000 00000001 .

Can you please help? The current code is below:

  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 (); }    

If you want to reverse only 32-bit numbers, The transfer technology, which separates each byte field by using bit-masks and logical 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) ;;   

You will now end up with the last value in temporary in 0x78563412 .

Update: OK, I'm looking at you code, and I'm focusing on the following:

  1. 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.
  2. Your 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

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -