作业题⽬: 位运算
完成下⾯任务(29 分)
密码系统有时间戳的需求,因此密码系统有实时钟芯⽚。假设实时钟鱿全局变量unsignted short TIME定义。
0. 在 Ubuntu 活 openEuler 中完成任务(推荐openEuler)
1. 16 位TIME 的含义
TIME (bit15) +++++ ++++++ +++++(bit0)
||||| |||||| ||||| ||||| ||||||
±------ second/2
||||| ±------------- minute
±------------------- hour
2. 使⽤位运算编写并调⽤下⾯函数 ,把当前时间(使⽤ C库函数获得)设置到 TIME 中,给出代码,使⽤ git
记录过程。(12 分)
void setHour(int h)
void setMinute(int m)
void setSecond(int s)
#include <stdio.h>
#include <time.h>
unsigned short TIME = 0;
void setHour(int h) {
TIME &= ~(0x1F << 10); // 清除小时位
TIME |= (h & 0x1F) << 10; // 设置小时位
}
void setMinute(int m) {
TIME &= ~(0x3F << 4); // 清除分钟位
TIME |= (m & 0x3F) << 4; // 设置分钟位
}
void setSecond(int s) {
TIME &= ~0x0F; // 清除秒位
TIME |= (s / 2) & 0x0F; // 设置秒位
}
int main() {
time_t t = time(NULL);
struct tm *tm = localtime(&t);
setHour(tm->tm_hour);
setMinute(tm->tm_min);
setSecond(tm->tm_sec);
printf("TIME: %04x\n", TIME);
return 0;
}
3. 使⽤位运算编写并调⽤下⾯函数 ,读取TIME 中的时间,给出代码,使⽤ git 记录过程。(12 分)
void setHour(int h)
void setMinute(int m)
void setSecond(int s)
#include <stdio.h>
unsigned short TIME = 0x1234; // 假设 TIME 已经被设置
int getHour() {
return (TIME >> 10) & 0x1F;
}
int getMinute() {
return (TIME >> 4) & 0x3F;
}
int getSecond() {
return (TIME & 0x0F) * 2;
}
int main() {
printf("Hour: %d\n", getHour());
printf("Minute: %d\n", getMinute());
printf("Second: %d\n", getSecond());
return 0;
}
4. 完成main.c测试模块,测试你完成的代码,给出代码,编译运⾏结果,使⽤ git 记录过程。(5ʼ)
获取系统时间设置TIME
读取你设置的结果
设置minute 为你学号最后两位
读取minute
#include <stdio.h>
#include <time.h>
unsigned short TIME = 0;
void setHour(int h) {
TIME &= ~(0x1F << 10); // 清除小时位
TIME |= (h & 0x1F) << 10; // 设置小时位
}
void setMinute(int m) {
TIME &= ~(0x3F << 4); // 清除分钟位
TIME |= (m & 0x3F) << 4; // 设置分钟位
}
void setSecond(int s) {
TIME &= ~0x0F; // 清除秒位
TIME |= (s / 2) & 0x0F; // 设置秒位
}
int getHour() {
return (TIME >> 10) & 0x1F;
}
int getMinute() {
return (TIME >> 4) & 0x3F;
}
int getSecond() {
return (TIME & 0x0F) * 2;
}
int main() {
time_t t = time(NULL);
struct tm *tm = localtime(&t);
setHour(tm->tm_hour);
setMinute(tm->tm_min);
setSecond(tm->tm_sec);
printf("TIME: %04x\n", TIME);
printf("Hour: %d\n", getHour());
printf("Minute: %d\n", getMinute());
printf("Second: %d\n", getSecond());
// 设置 minute 为学号最后两位
int studentIDLastTwoDigits = 42; // 假设学号最后两位是 42
setMinute(studentIDLastTwoDigits);
printf("Updated Minute: %d\n", getMinute());
return 0;
}
然后编译运行并传到git
root@LAPTOP-PRC71A0C:~/wzy/ch2/main# vim main.c
root@LAPTOP-PRC71A0C:~/wzy/ch2/main# gcc -o main main.c
root@LAPTOP-PRC71A0C:~/wzy/ch2/main# ls
main main.c
root@LAPTOP-PRC71A0C:~/wzy/ch2/main# ./main
TIME: 2c95
Hour: 11
Minute: 9
Second: 10
Updated Minute: 42
root@LAPTOP-PRC71A0C:~/wzy/ch2/main# git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint: git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint: git branch -m <name>
Initialized empty Git repository in /root/wzy/ch2/main/.git/
root@LAPTOP-PRC71A0C:~/wzy/ch2/main# git add main.c
root@LAPTOP-PRC71A0C:~/wzy/ch2/main# ls
main main.c
root@LAPTOP-PRC71A0C:~/wzy/ch2/main# git commit -m "Initial commit with time setting and reading functions"
[master (root-commit) 283e1f5] Initial commit with time setting and reading functions
Committer: root <root@LAPTOP-PRC71A0C>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:
git config --global --edit
After doing this, you may fix the identity used for this commit with:
git commit --amend --reset-author
1 file changed, 54 insertions(+)
create mode 100644 main.c
root@LAPTOP-PRC71A0C:~/wzy/ch2/main# git log
commit 283e1f5b3548782ef778befeea24454c8ddfce17 (HEAD -> master)
Author: root <root@LAPTOP-PRC71A0C>
Date: Tue Oct 8 11:11:18 2024 +0800
Initial commit with time setting and reading functions