0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Arduino UNOでビットの並びを逆転して遊ぶ。(0bABCD EFGH->0bHGFE DCBA)

Last updated at Posted at 2024-04-11

目的
8ビットの上位、下位を逆転して遊ぶ
3回シフトする方法を選択
ビットリバースと言うらしい。
ビット順序反転
(逆転再生の逆転)

1回目 ABCD EFGH -> EFGH ABCD

2回目 EFGH ABCD -> GHEF CDAB

3回目 GHEF CDAB -> HGFE DCBA

オンラインコンパイラ


#include <iostream>
using namespace std;
int main(void){
    // Your code here!
    
    int x=0b10000000;
    printf("[%02x]\n",x);
    
    x=(x >> 4) | ( (x & 0x0f) << 4 );
    printf("[%02x]\n",x);
    
    x=((x & 0b11001100) >> 2) | ( (x & 0b00110011) << 2 );
    printf("[%02x]\n",x);
    

    x=((x & 0b10101010) >> 1) | ( (x & 0b01010101) << 1 );
    printf("[%02x]\n",x);
    
    
    
}


結果


[80]
[08]
[02]
[01]

arduino

結果

o_coq026.jpg

プログラム




//0bABCD EFGH -> 0bHGFE DCBA
//0bABCD_EFGH__0bHGFE_DCBA_UNO_1


// the setup routine runs once when you press reset:
void setup() {

  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);

}

// the loop routine runs over and over again forever:
void loop() {

  int x = 0b10000000;

  // print out the value:
  Serial.println(x, HEX);

  x = (x >> 4) | ( (x & 0x0f) << 4 );
  // print out the value:
  Serial.println(x, HEX);

  x = ((x & 0b11001100) >> 2) | ( (x & 0b00110011) << 2 );
  // print out the value:
  Serial.println(x, HEX);

  x = ((x & 0b10101010) >> 1) | ( (x & 0b01010101) << 1 );
  // print out the value:
  Serial.println(x, HEX);

  delay(1000);        // delay in between reads for stability
}



0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?