课上测试:位运算

作业题⽬: 位运算

完成下⾯任务(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

作业提交要求 (1’)

0. 记录实践过程和 AI 问答过程,尽量不要截图,给出⽂本内容

1. (选做)推荐所有作业托管到 gitee或 github 上

2. (必做)提交作业 markdown⽂档,命名为“学号-姓名-作业题⽬.md”

3. (必做)提交作业 markdown⽂档转成的 PDF ⽂件,命名为“学号-姓名-作业题⽬.pdf”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值