2
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?

int (32bit) を 4byte に分割して遊ぶ。(戻しも)(一番下にコメントあり)

Last updated at Posted at 2025-01-14

x M5NanoC6での予定は、未定

目的
ビット操作してレジスターから取り出す

いろいろ
ESP-NOWを使った温度の電送で必要
主にADC(0から4095)まで等を想定
最上バイトには、ちゃんとマスクしをしています。
オーバーブローに関しては、規格上は、未定義 (自己責任)

オンラインコンパイラ


#include <iostream>
using namespace std;
int main(void){
    // Your code here!
    
    printf("\nSTART\n");
    
    //送信バッファー int(32bit)
    unsigned char ac[] = {0x00, 0x00, 0x00, 0x00};
    
    int s = 0x12345678;    //センサーの値
    
    printf("[%x]\n",s);
    
    ac[3] = s & 0xff; s = s >> 8;
    ac[2] = s & 0xff; s = s >> 8;
    ac[1] = s & 0xff; s = s >> 8;
    ac[0] = s & 0xff;
    
    printf("[%x][%x][%x][%x]\n",ac[0],ac[1],ac[2],ac[3]);
    

    s = 0;
    s =     ac[0]; s = s << 8;
    s = s | ac[1]; s = s << 8;
    s = s | ac[2]; s = s << 8;
    s = s | ac[3];
    
    printf("\n");
    printf("[%x]\n",s);
    

    
    
}




START
[12345678]
[12][34][56][78]

[12345678]


おまけ(負の数)



#include <iostream>
using namespace std;
int main(void){
    // Your code here!
    
    printf("\nSTART\n");
    
    //送信バッファー int(32bit)
    unsigned char ac[] = {0x00, 0x00, 0x00, 0x00};
    
    int s = -2;    //センサーの値
    
    printf("[%d]\n",s);
    
    ac[3] = s & 0xff; s = s >> 8;
    ac[2] = s & 0xff; s = s >> 8;
    ac[1] = s & 0xff; s = s >> 8;
    ac[0] = s & 0xff;
    
    printf("[%x][%x][%x][%x]\n",ac[0],ac[1],ac[2],ac[3]);
    

    s = 0;
    s =     ac[0]; s = s << 8;
    s = s | ac[1]; s = s << 8;
    s = s | ac[2]; s = s << 8;
    s = s | ac[3];
    
    printf("\n");
    printf("[%d]\n",s);
    
}



START
[-2]
[ff][ff][ff][fe]

[-2]

おまけ2(ポインターによるメモリー操作)




#include <iostream>
using namespace std;
int main(void){
    // Your code here!
    
    printf("\nSTART\n");
    
    //送信バッファー int(32bit)
    unsigned char ac[] = {0x00, 0x00, 0x00, 0x00};
    
    int s = -2;    //センサーの値
    
    printf("[%d]\n",s);

    
    unsigned char *c;
    c = (unsigned char *)(&s);
    ac[0]=c[0];
    ac[1]=c[1];
    ac[2]=c[2];
    ac[3]=c[3];
    
    //ac[3] = s & 0xff; s = s >> 8;
    //ac[2] = s & 0xff; s = s >> 8;
    //ac[1] = s & 0xff; s = s >> 8;
    //ac[0] = s & 0xff;
    
    printf("[%x][%x][%x][%x]\n",ac[0],ac[1],ac[2],ac[3]);
    
 

    
    
    int *p;
    p = (int *)(ac);
    s = *p; //メモリコピー
    


    //s = 0;
    //s =     ac[0]; s = s << 8;
    //s = s | ac[1]; s = s << 8;
    //s = s | ac[2]; s = s << 8;
    //s = s | ac[3];
    
    printf("\n");
    printf("[%d]\n",s);
    
}




START
[-2]
[fe][ff][ff][ff]

[-2]


2
0
11

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
2
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?