1 前言
最近博主在后臺收到一位朋友的咨詢,說他最近參加了一場技術(shù)面試,有這么一道筆試題:
請使用C語言的宏定義實現(xiàn)一個功能,求得某個整型數(shù)M在N字節(jié)對齊的時,它的值大小。
說明:
1.M是一個非負整數(shù);
2.N是2的整數(shù)倍,值可能是1,2,4,8,16等等。
要求:
1.不得使用除法(/);
2.不能使用函數(shù)實現(xiàn),只能用宏實現(xiàn);
3.自行設(shè)計測試用例,明確得出測試用例執(zhí)行成功與否。
2 代碼實現(xiàn)
剛好,今天比較清閑,茶余飯后,順手擼了一把代碼:
#include
#include
/* max test number for aligned */
#define MAX_TEST_NUM 1000
/* default for 8 bytes */
#define DEF_ALIGNED_BYTES 8
/* n = 2/4/6/8/16/... */
#define GET_ALIGNED_2_POWER_NUM(num, n) (((num) + (n) - 1) & (~((n) - 1)))
int main(int argc, const char *argv[])
{
int i = 0;
int max_cnt = MAX_TEST_NUM;
int aligned_bytes = DEF_ALIGNED_BYTES;
int aligned_num;
if (argc > 1) {
aligned_bytes = atoi(argv[1]);
if (aligned_bytes < 0) {
printf("error aligned_bytes input !\r\n");
return -1;
}
}
/* test cases start */
for (i = 0; i < max_cnt; i++) {
aligned_num = GET_ALIGNED_2_POWER_NUM(i, aligned_bytes);
//printf("%-4d ALIGNED %d => %-4d\r\n", i, aligned_bytes, aligned_num);
if (aligned_num % aligned_bytes != 0) {
printf("error aligned_num get: %-4d ALIGNED %d => %-4d\r\n", i, aligned_bytes, aligned_num);
printf("test cases (0 ~ %d) ALIGNED %d [ FAIL ] !\r\n", max_cnt, aligned_bytes);
return -1;
}
}
printf("test cases (0 ~ %d) ALIGNED %d [ OK ] !\r\n", max_cnt, aligned_bytes);
return 0;
}
3 代碼驗證
ubuntu下面使用gcc編譯,得到可執(zhí)行文件:
gcc -o test main.c
跑了下測試用例:
recan@ubuntu:~/llc/aligned_macro_test$ ./test 2
test cases (0 ~ 1000) ALIGNED 2 [ OK ] !
recan@ubuntu:~/llc/aligned_macro_test$
recan@ubuntu:~/llc/aligned_macro_test$ ./test 4
test cases (0 ~ 1000) ALIGNED 4 [ OK ] !
recan@ubuntu:~/llc/aligned_macro_test$
recan@ubuntu:~/llc/aligned_macro_test$ ./test 8
test cases (0 ~ 1000) ALIGNED 8 [ OK ] !
recan@ubuntu:~/llc/aligned_macro_test$
recan@ubuntu:~/llc/aligned_macro_test$ ./test 16
test cases (0 ~ 1000) ALIGNED 16 [ OK ] !
recan@ubuntu:~/llc/aligned_macro_test$
recan@ubuntu:~/llc/aligned_macro_test$ ./test 32
test cases (0 ~ 1000) ALIGNED 32 [ OK ] !
recan@ubuntu:~/llc/aligned_macro_test$
recan@ubuntu:~/llc/aligned_macro_test$ ./test 64
test cases (0 ~ 1000) ALIGNED 64 [ OK ] !
recan@ubuntu:~/llc/aligned_macro_test$
recan@ubuntu:~/llc/aligned_macro_test$ ./test 128
test cases (0 ~ 1000) ALIGNED 128 [ OK ] !
recan@ubuntu:~/llc/aligned_macro_test$
recan@ubuntu:~/llc/aligned_macro_test$ ./test 256
test cases (0 ~ 1000) ALIGNED 256 [ OK ] !
recan@ubuntu:~/llc/aligned_macro_test$
recan@ubuntu:~/llc/aligned_macro_test$
recan@ubuntu:~/llc/aligned_macro_test$
recan@ubuntu:~/llc/aligned_macro_test$ ./test 3
error aligned_num get: 1 ALIGNED 3 => 1
test cases (0 ~ 1000) ALIGNED 3 [ FAIL ] !
recan@ubuntu:~/llc/aligned_macro_test$
recan@ubuntu:~/llc/aligned_macro_test$ ./test 5
error aligned_num get: 1 ALIGNED 5 => 1
test cases (0 ~ 1000) ALIGNED 5 [ FAIL ] !
recan@ubuntu:~/llc/aligned_macro_test$
recan@ubuntu:~/llc/aligned_macro_test$ ./test 7
error aligned_num get: 1 ALIGNED 7 => 1
test cases (0 ~ 1000) ALIGNED 7 [ FAIL ] !
recan@ubuntu:~/llc/aligned_macro_test$
recan@ubuntu:~/llc/aligned_macro_test$ ./test 167
error aligned_num get: 1 ALIGNED 167 => 1
test cases (0 ~ 1000) ALIGNED 167 [ FAIL ] !
recan@ubuntu:~/llc/aligned_macro_test$
recan@ubuntu:~/llc/aligned_macro_test$
recan@ubuntu:~/llc/aligned_macro_test$ ./test 79
error aligned_num get: 1 ALIGNED 79 => 1
test cases (0 ~ 1000) ALIGNED 79 [ FAIL ] !
recan@ubuntu:~/llc/aligned_macro_test$
recan@ubuntu:~/llc/aligned_macro_test$
從log上看,基本實現(xiàn)了功能,對于非對齊的數(shù)值,都能做出準確的判斷。
大家記住這個宏定義吧!
/* n = 2/4/6/8/16/... */
#define GET_ALIGNED_2_POWER_NUM(num, n) (((num) + (n) - 1) & (~((n) - 1)))
4 題外話
題外話,如果把題目中的N改為【任意非負整數(shù)】呢?
又該如何改造這個宏定義呢?
下次有時間,我們再試試看!
5 更多分享
歡迎關(guān)注我的github倉庫01workstation,日常分享一些開發(fā)筆記和項目實戰(zhàn),歡迎指正問題。
同時也非常歡迎關(guān)注我的專欄:有問題的話,可以跟我討論,知無不答,謝謝大家。
-
C語言
+關(guān)注
關(guān)注
180文章
7630瀏覽量
140182 -
宏定義
+關(guān)注
關(guān)注
0文章
51瀏覽量
9175
發(fā)布評論請先 登錄
評論