女人自慰AV免费观看内涵网,日韩国产剧情在线观看网址,神马电影网特片网,最新一级电影欧美,在线观看亚洲欧美日韩,黄色视频在线播放免费观看,ABO涨奶期羡澄,第一导航fulione,美女主播操b

0
  • 聊天消息
  • 系統消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發帖/加入社區
會員中心
創作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示

【C語言進階】面試題:請使用宏定義實現字節對齊

嵌入式物聯網開發 ? 來源:嵌入式物聯網開發 ? 作者:嵌入式物聯網開發 ? 2022-07-11 09:21 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

1 前言

最近博主在后臺收到一位朋友的咨詢,說他最近參加了一場技術面試,有這么一道筆試題:

請使用C語言的宏定義實現一個功能,求得某個整型數M在N字節對齊的時,它的值大小。

說明:
1.M是一個非負整數;
2.N是2的整數倍,值可能是1,2,4,8,16等等。

要求:
1.不得使用除法(/);
2.不能使用函數實現,只能用宏實現;
3.自行設計測試用例,明確得出測試用例執行成功與否。

2 代碼實現

剛好,今天比較清閑,茶余飯后,順手擼了一把代碼:

#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編譯,得到可執行文件:

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上看,基本實現了功能,對于非對齊的數值,都能做出準確的判斷。

大家記住這個宏定義吧!

/* n = 2/4/6/8/16/... */
#define GET_ALIGNED_2_POWER_NUM(num, n)        (((num) + (n) - 1) & (~((n) - 1))) 

4 題外話

題外話,如果把題目中的N改為【任意非負整數】呢?

又該如何改造這個宏定義呢?

下次有時間,我們再試試看!

5 更多分享

歡迎關注我的github倉庫01workstation,日常分享一些開發筆記和項目實戰,歡迎指正問題。

同時也非常歡迎關注我的專欄:有問題的話,可以跟我討論,知無不答,謝謝大家。

聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。 舉報投訴
  • C語言
    +關注

    關注

    180

    文章

    7632

    瀏覽量

    141659
  • 宏定義
    +關注

    關注

    0

    文章

    51

    瀏覽量

    9227
收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評論

    相關推薦
    熱點推薦

    【經典面試題請使用C語言編程實現對IPV4地址的合法性判斷

    【經典面試題請使用C語言編程實現對IPV4地址的合法性判斷
    的頭像 發表于 05-16 15:23 ?2286次閱讀

    C語言面試題大全

    C語言面試題大全{:1:}{:2:}{:1:}{:1:}{:1:}{:1:}
    發表于 04-10 20:51

    C語言 經典面試題

    C語言經典面試題目.doc
    發表于 08-05 22:03

    12個常見的C語言面試題

    12個C語言面試題,涉及指針、進程、運算、結構體、函數、內存
    發表于 12-31 06:36

    c語言面試題,c++面試題下載

    c語言面試題,c++面試題1. static有什么用途?(請至少說明兩種) 1) 限制變量的作用域 2) 設置變量的存儲域 2.&
    發表于 10-22 11:19 ?5次下載

    c語言面試題

    c語言面試題集(單片機)C language problem(20151125084232)
    發表于 12-18 14:05 ?9次下載

    c語言面試題

    c語言面試題
    發表于 11-05 16:48 ?0次下載

    C語言經典面試題

    面試題
    發表于 12-20 22:41 ?0次下載

    C語言經典面試題

    C語言 經典面試題
    發表于 01-05 11:27 ?0次下載

    單片機C語言面試題的詳細資料合集

    本文檔的主要內容詳細介紹的是單片機C語言面試題的詳細資料合集。
    發表于 07-24 17:37 ?24次下載
    單片機<b class='flag-5'>C</b><b class='flag-5'>語言</b><b class='flag-5'>面試題</b>的詳細資料合集

    解析C語言結構體字節如何對齊

    01 默認字節對齊 C語言結構體字節對齊是老生常談的問題了,也是高頻
    的頭像 發表于 06-12 17:42 ?3396次閱讀

    C語言經典面試題】求數組元素的個數的定義

    經典面試題,有必要了解下!
    的頭像 發表于 10-02 11:58 ?3954次閱讀
    【<b class='flag-5'>C</b><b class='flag-5'>語言</b>經典<b class='flag-5'>面試題</b>】求數組元素的個數的<b class='flag-5'>宏</b><b class='flag-5'>定義</b>

    C語言進階面試題請使用代碼判斷主機存儲屬于大端模式還是小端模式?

    經典面試題,有必要了解下!
    的頭像 發表于 10-02 11:56 ?2719次閱讀
    【<b class='flag-5'>C</b><b class='flag-5'>語言</b><b class='flag-5'>進階</b>】<b class='flag-5'>面試題</b>:<b class='flag-5'>請使用</b>代碼判斷主機存儲屬于大端模式還是小端模式?

    分享10道有趣的嵌入式C語言面試題及答案

    10個C語言面試題,涉及指針、進程、運算、結構體、函數、內存,看看你能做出幾個!
    的頭像 發表于 05-09 10:54 ?3663次閱讀

    c語言面試題集(完整版)

    電子發燒友網站提供《c語言面試題集(完整版).pdf》資料免費下載
    發表于 10-20 11:20 ?4次下載
    <b class='flag-5'>c</b><b class='flag-5'>語言</b><b class='flag-5'>面試題</b>集(完整版)