a/1024与a>>10的区别

本文讨论了在DSP优化中使用移位操作来替代除法运算的方法以提高效率,特别关注了当变量可能为负数时如何正确地进行移位操作。介绍了通过判断符号位并结合绝对值移位来解决负数情况下的不准确问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在DSP优化过程中通常有一般都用移位来做除法运算,从而提高运算速度,但是当变量为负值时,移位得到的结果跟除法得到的结果不一样。

        int   a;

        int   b;

 

        b = a/1024;

可以写为:

        b = a>>10;

 

如果a可以为负值,就不能这样简单的改写了,应该这样改:

       int sign = a >> 31;

 

       b = (abs(a)>>10)*sign;

 

这样还是很耗时,但也找不到更好的办法。

将以下代码修改为响应式布局修改页面分辨率时布局仍然保持原样,不出现滚动条<template> <div id="mainContainer" class="energy-platform"> <!-- 顶部标题 --> <div class="header"> <h1 class="company-name">XX公司</h1> <h2 class="platform-name">XX云综合管理平台</h2> <div class="gradient-bar"></div> <!-- 渐变导航条 --> </div> <!-- 应用菜单 --> <div class="app-container"> <el-row :gutter="20"> <el-col v-for="(app, index) in apps" :key="index" :xs="24" :sm="12" :md="6" :lg="6"> <div v-if="app.url=='index'" target="_blank" class="app-card" @mouseenter="hoverIndex = index" @mouseleave="hoverIndex = -1" @click="goIndex" :style="{ backgroundColor: hoverIndex === index ? '#c5edff' : '#fff' }"> <div class="app-icon"> <img :src="require(`../assets/image/${app.icon}.png`)" :alt="app.name" class="img-item2"> </div> <div class="app-title">{{ app.name }}</div> </div> <a v-else :href="app.url" target="_blank" class="app-card" @mouseenter="hoverIndex = index" @mouseleave="hoverIndex = -1" :style="{ backgroundColor: hoverIndex === index ? '#c5edff' : '#fff' }"> <div class="app-icon"> <img :src="require(`../assets/image/${app.icon}.png`)" :alt="app.name" class="img-item2"> </div> <div class="app-title">{{ app.name }}</div> </a> </el-col> </el-row> </div> </div> </template> <script> export default { name: "Home", data() { return { hoverIndex: -1, apps: [{ name: '智慧应用', icon: 'el-icon-cpu', url: 'index' }, { name: '运行监测', icon: 'el-icon-monitor', url: '' }, { name: 'AI仿真', icon: 'el-icon-data-analysis', url: '' }, { name: '数据中台', icon: 'el-icon-data-board', url: '' } ], redirect: undefined, devicePixelRatio: null, contentStyle: {} } }, methods: { goIndex() { this.$router.replace({ path: "Index" }) } } } </script> <style scoped> .energy-platform { box-sizing: border-box; padding: 8rem 10rem; width: 100%; /* height: 100vh; */ min-height: 100vh; background-image: url('../assets/image/bg.png'); /* 背景图片设置 */ background-size: 100% 100%; background-position: center; background-repeat: no-repeat; filter: brightness(1); } .header { text-align: left; font-style: italic; color: #fff; font-size: 40px; } .company-name { margin-bottom: 30px; } .company-name, .platform-name { text-align: left; font-weight: bold; margin-bottom: 20px; color: #fff; } .gradient-bar { height: 4px; background: linear-gradient(to right, #29b0dd, #2aaed9, #ffffff); width: 260px; margin-bottom: 90px; } .app-container { margin: 0 auto; } .app-card { height: 260px; width: 80%; background: #fff; border-radius: 8px; display: flex; flex-direction: column; justify-content: center; align-items: center; cursor: pointer; transition: all 0.3s ease; box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1); margin-bottom: 20px; } .img-item2 { width: 100%; } .app-card:hover { transform: translateY(-5px); box-shadow: 0 6px 18px 0 rgba(0, 0, 0, 0.15); } .app-icon { font-size: 50px; color: #333; margin-bottom: 40px; transition: all 0.3s ease; } .app-card:hover .app-icon { color: #fff; } .app-title { font-size: 22px; font-weight: bold; color: #333; transition: all 0.3s ease; } .app-card:hover .app-title { color: #000; } /* 响应式调整 */ @media (max-width: 768px) { .company-name { font-size: 40px; } .platform-name { font-size: 20px; } .app-card { height: 120px; width: 100%; } .app-icon { font-size: 30px; margin-bottom: 10px; } .app-title { font-size: 16px; } .gradient-bar { margin-bottom: 40px; } } /* 响应式调整 */ @media (max-width: 1366px) { .energy-platform { padding: 150px 100px; } .app-card { width: 90%; } } /* 响应式调整 */ @media (max-width: 1024px) { .energy-platform { padding: 150px 200px; } .app-card { width: 100%; } } </style>
07-16
#include <iostream> #include <cstring> using namespace std; class String { public: // 默认构造函数:创建空字符串 String() : p(new char[1]) { p[0] = '\0'; } // 从C字符串构造 String(const char* s) { p = new char[strlen(s) + 1]; strcpy(p, s); } // 拷贝构造函数(新添加) String(const String& s) { p = new char[s.Length() + 1]; strcpy(p, s.p); } // 析构函数 ~String() { delete[] p; } // 赋值运算符(新添加) String& operator=(const String& s) { if(this != &s) { delete[] p; p = new char[s.Length() + 1]; strcpy(p, s.p); } return *this; } // 拼接运算符+ String operator+(const String& a) const { String result; delete[] result.p; // 释放默认构造的空间 result.p = new char[Length() + a.Length() + 1]; strcpy(result.p, p); strcat(result.p, a.p); return result; } // 复合赋值运算符+= String& operator+=(const String& a) { char* temp = new char[Length() + a.Length() + 1]; strcpy(temp, p); strcat(temp, a.p); delete[] p; p = temp; return *this; } // 相等运算符 bool operator==(const String& a) const { return strcmp(p, a.p) == 0; } // 小于运算符 bool operator<(const String& a) const { return strcmp(p, a.p) < 0; } // 输入运算符 friend istream& operator>>(istream& is, String& b) { char buffer[1024]; // 临时缓冲区 is >> buffer; delete[] b.p; // 释放原有内存 b.p = new char[strlen(buffer) + 1]; strcpy(b.p, buffer); return is; } // 输出运算符 friend ostream& operator<<(ostream& os, const String& b) { os << b.p; return os; } // 下标运算符 char& operator[](int n) { return p[n]; } // 常量下标运算符(新添加) const char& operator[](int n) const { return p[n]; } // 获取长度 int Length() const { return strlen(p); } private: char* p; }; int main() { String s1("Help!"), s2("Good!"), s3(s2), s4, s5; cout << "s1=" << s1 << endl; s3 = "Hello!"; // 需要赋值运算符支持 cout << "s3=" << s3 << endl; s3 = s2; // 需要赋值运算符支持 cout << "s3=" << s3 << endl; s3 += s2; // 需要operator+= cout << "s3=" << s3 << endl; cin >> s4; cout << "s4=" << s4 << endl; s5 = s3 + s4; // 需要operator+和赋值运算符 cout << "s5=" << s5 << endl; s5[0] = 'g'; cout << "s5=" << s5 << endl; cout << "strlen(s5)=" << s5.Length() << endl; cout << boolalpha << (s3 == s1) << endl; cout << boolalpha << (s3 < s1) << endl; }
06-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值