OSDN Git Service

addid library source 20140221
[zither/ds-zither.git] / sources / lib / libms / shift8_updown_out.c
1
2
3
4 // 一つの32Bit整数に4つの8Bit整数を含めて取り出す
5 // int main(void)
6 // {
7 // int bar = 0;
8 // int ab = 0;
9 // int abc = 0;
10 // int abcd = 0;
11 //
12 // int a = 10;
13 // int b = 20;
14 // int c = 30;
15 // int d = 40;
16 //
17 //
18 // int mask_4 = 0xff000000;
19 // int mask_3 = 0x00ff0000;
20 // int mask_2 = 0x0000ff00;
21 // int mask_1 = 0x000000ff;
22 //
23 // ab = ((a << 8) + b);
24 // abc = ((ab << 8) + c);
25 // abcd = ((abc << 8) + d);
26 //
27 //
28 // printf("a = %d, b = %d, c = %d, d = %d, ab = %d, abc = %d, abcd = %d\n", a, b, c, d, ab, abc, abcd);
29 // printf("ax = %x, bx = %x, cx = %x, dx = %x, abx = %x, abcx = %x, abcdx = %x\n", a, b, c, d, ab, abc, abcd);
30 //
31 // bar = ((abcd & mask_4) >> 24);
32 // printf("mask4 %x: %d\n", bar, bar);
33 //
34 // bar = ((abcd & mask_3) >> 16);
35 // printf("mask3 %x: %d\n", bar, bar);
36 //
37 // bar = ((abcd & mask_2) >> 8);
38 // printf("mask2 %x: %d\n", bar, bar);
39 //
40 // bar = (abcd & mask_1);
41 // printf("mask1 %x: %d\n", bar, bar);
42 //
43 // return 0;
44 // }
45 //
46
47
48
49
50
51 #include <stdio.h>
52
53 int shift8_updown_out(int base, char ch)
54 {
55
56 int mask_to =    0xff000000;
57 int mask_mi =    0x00ff0000;
58 int mask_up =   0x0000ff00;
59 int mask_down = 0x000000ff;
60
61 int ret = 0;
62
63 if(base < 0){ base = 0; }
64
65 // 32ビット整数を右に24ビットシフトした位置を0にして8ビット整数を読む
66 if(ch == 't'){
67    ret = ((base & mask_to) >> 24);
68    }
69 // 32ビット整数を右に16ビットシフトした位置を0にして8ビット整数を読む
70 else if(ch == 'm'){
71    ret = ((base & mask_mi) >> 16);
72    }
73 // 32ビット整数を右に8ビットシフトした位置を0にして8ビット整数を読む
74 else if(ch == 'u'){
75    ret = ((base & mask_up) >> 8);
76    }
77 else if(ch == 'd'){
78    ret = (base & mask_down);
79    }
80 else{
81    ret = -1;
82    }
83
84 return ret;
85
86
87
88
89
90
91
92
93
94