IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> 【C语言面试题】请使用宏定义实现字节对齐! -> 正文阅读

[C++知识库]【C语言面试题】请使用宏定义实现字节对齐!

最近博主在后台收到一位朋友的咨询,说他最近参加了一场技术面试,有这么一道笔试题:

请使用C语言的宏定义实现一个功能,求得某个整型数M在N字节对齐的时,它的值大小。

说明:
1.M是一个非负整数;
2.N是2的整数倍,值可能是1,2,4,8,16等等。

要求:
1.不得使用除法(/);
2.不能使用函数实现,只能用宏实现;
3.自行设计测试用例,明确得出测试用例执行成功与否。

刚好,今天比较清闲,茶余饭后,顺手撸了一把代码:


#include <stdio.h>
#include <stdlib.h>

/* 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;
}

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)))	

题外话,如果把题目中的N改为【任意非负整数】呢?

又该如何改造这个宏定义呢?

下次有时间,我们再试试看!

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章           查看所有文章
加:2021-08-05 17:09:10  更:2021-08-05 17:11:47 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/8 11:43:21-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码