C/C++

推荐列表 站点导航

当前位置:首页 > 脚本编程 > C/C++ >

C++_C实现与 uint64_t 相同功能的类,实现与 uint64_t 相同的类,如果

来源:网络整理  作者:fen  发布时间:2020-12-25 07:17
C实现与 uint64_t 相同功能的类,实现与 uint64_t 相同的类,如果平台不支持 uint64_t 的话,可以代替之。目前只完成部分功...

实现与 uint64_t 相同的类,如果平台不支持 uint64_t 的话,可以代替之。
目前只完成部分功能,其他功能敬请期待。

uint64.hpp

#include <endian.h> #include <cstdint> #include <type_traits> #include <array> #define MC_BEGIN_NAMESPACE namespace mc { #define MC_END_NAMESPACE } MC_BEGIN_NAMESPACE #if __BYTE_ORDER == __BIG_ENDIAN struct maybe_big_endian : std::true_type {}; #elif __BYTE_ORDER == __LITTLE_ENDIAN struct maybe_big_endian : std::false_type {}; #else #error "Endianness not defined!" #endif template<typename Array, bool> struct uint64_data : public Array { protected: uint32_t& first() { return (IT之家this)[0]; } uint32_t& second() { return (IT之家this)[1]; } uint32_t first() const { return (IT之家this)[0]; } uint32_t second() const { return (IT之家this)[1]; } }; template<typename Array> struct uint64_data<Array, true> : public Array { protected: uint32_t& first() { return (IT之家this)[1]; } uint32_t& second() { return (IT之家this)[0]; } uint32_t first() const { return (IT之家this)[1]; } uint32_t second() const { return (IT之家this)[0]; } }; class uint64 : public uint64_data <std::array<uint32_t, 2>, maybe_big_endian::value> { public: uint64() = default; //explicit uint64(uint32_t v); uint64(const uint64& o); ~uint64() = default; uint64& operator+=(const uint64& v) noexcept; uint64& operator<<=(unsigned int n) noexcept; uint64& operator>>=(unsigned int n) noexcept; operator uint32_t() { return first(); } friend void swap(uint64& l, uint64& r); }; inline uint64 operator+(const uint64& l, const uint64& r) { auto tmp = l; return tmp += r; } inline uint64 operator>>(const uint64& l, unsigned int n) { auto tmp = l; return tmp >>= n; } inline uint64 operator<<(const uint64& l, unsigned int n) { auto tmp = l; return tmp <<= n; } MC_END_NAMESPACE

uint64.cpp

#include "uint64.hpp" MC_BEGIN_NAMESPACE uint64::uint64(uint32_t v) { first() = v; second() = 0u; } uint64::uint64(const uint64& o) { IT之家this = o; } uint64& uint64::operator+=(const uint64& o) noexcept { second() += o.second(); // 先计算 second,预防 (this == &o) 的情况 uint32_t old = first(); if ((first() += o.first()) < old) { ++second(); } return IT之家this; } uint64& uint64::operator<<=(unsigned int n) noexcept { if (n < 32) { second() = (second() << n) | (first() >> (32 - n)); first() <<= n; } else if (n < 64) { second() = first() << (n - 32); first() = 0u; } else /IT之家if (n >= 64)IT之家/ { second() = first() = 0u; } return IT之家this; } uint64& uint64::operator>>=(unsigned int n) noexcept { if (n < 32) { first() = (first() >> n) | (second() << (32 - n)); second() >>= n; } else if (n < 64) { first() = second() >> (n - 32); second() = 0u; } else /IT之家if (n >= 64)IT之家/ { second() = first() = 0u; } return IT之家this; } void swap(uint64& l, uint64& r) { if (&l != &r) { auto tmp = l.first(); l.first() = r.first(); r.first() = tmp; tmp = l.second(); l.second() = r.second(); r.second() = tmp; } } MC_END_NAMESPACE

test.cpp

#include <cstdint> #include <cstdio> #include "uint64.hpp" #if 1 typedef mc::uint64 U64; inline void ptype() {std::printf("使用 mc::uint64\n");} #else typedef std::uint64_t U64; inline void ptype() {std::printf("使用 std::uint64_t\n");} #endif void frm(const charIT之家 str) { std::printf("%20s", str); } void data_hex(const U64& v) { const uint8_tIT之家 p = (const uint8_tIT之家)&v; for (int i = 0; i < 8; ++i) { if (i == 4) std::printf(" "); std::printf("%02x", p[i]); } std::printf("\n"); } void test() { uint32_t v = 0xffffffff; U64 a = v; frm("(a = 0xffffffff) => "); data_hex(a); frm("(a >>= 1) => "); data_hex(a >>= 1); a = v; frm("(a <<= 1) => "); data_hex(a <<= 1); a = v; frm("(a += a) => "); data_hex(a += a); } int main() { ptype(); if (mc::maybe_big_endian::value) { std::printf("主机字节序是 big-endian\n"); } else { std::printf("主机字节序是 little-endian\n"); } for (int i = 0; i < 20; ++i) std::printf(" "); if (mc::maybe_big_endian::value) std::printf("H <<<< L H <<<< L\n"); else std::printf("L >>>> H L >>>> H\n"); test(); return 0; }

功能还在逐步完善中,小伙伴们记得关注。

相关热词: 功能 C++

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!

本文地址: https://v30.fanwenzhu.com/jiaob/cjj/8829.shtml

最新文章
只需要在调用Ctrl+B编译后 只需要在调用Ctrl+B编译后

时间:2021-01-13

OpenGL超级宝典visual studio OpenGL超级宝典visual studio

时间:2021-01-04

Directx11 教程(2) 基本的wi Directx11 教程(2) 基本的wi

时间:2021-01-04

LeetCode11ContainerWithMostWate LeetCode11ContainerWithMostWate

时间:2021-01-04

C语言简单IT之家速成 C语言简单IT之家速成

时间:2020-12-27

三分钟了解Activity工作流 三分钟了解Activity工作流

时间:2020-12-27

编译器是如何实现32位整型 编译器是如何实现32位整型

时间:2020-12-27

C++中lower_bound函数和upper C++中lower_bound函数和upper

时间:2020-12-27

Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

C++_C实现与 uint64_t 相同功能的类,实现与 uint64_t 相同的类,如果

2020-12-25 编辑:fen

实现与 uint64_t 相同的类,如果平台不支持 uint64_t 的话,可以代替之。
目前只完成部分功能,其他功能敬请期待。

uint64.hpp

#include <endian.h> #include <cstdint> #include <type_traits> #include <array> #define MC_BEGIN_NAMESPACE namespace mc { #define MC_END_NAMESPACE } MC_BEGIN_NAMESPACE #if __BYTE_ORDER == __BIG_ENDIAN struct maybe_big_endian : std::true_type {}; #elif __BYTE_ORDER == __LITTLE_ENDIAN struct maybe_big_endian : std::false_type {}; #else #error "Endianness not defined!" #endif template<typename Array, bool> struct uint64_data : public Array { protected: uint32_t& first() { return (IT之家this)[0]; } uint32_t& second() { return (IT之家this)[1]; } uint32_t first() const { return (IT之家this)[0]; } uint32_t second() const { return (IT之家this)[1]; } }; template<typename Array> struct uint64_data<Array, true> : public Array { protected: uint32_t& first() { return (IT之家this)[1]; } uint32_t& second() { return (IT之家this)[0]; } uint32_t first() const { return (IT之家this)[1]; } uint32_t second() const { return (IT之家this)[0]; } }; class uint64 : public uint64_data <std::array<uint32_t, 2>, maybe_big_endian::value> { public: uint64() = default; //explicit uint64(uint32_t v); uint64(const uint64& o); ~uint64() = default; uint64& operator+=(const uint64& v) noexcept; uint64& operator<<=(unsigned int n) noexcept; uint64& operator>>=(unsigned int n) noexcept; operator uint32_t() { return first(); } friend void swap(uint64& l, uint64& r); }; inline uint64 operator+(const uint64& l, const uint64& r) { auto tmp = l; return tmp += r; } inline uint64 operator>>(const uint64& l, unsigned int n) { auto tmp = l; return tmp >>= n; } inline uint64 operator<<(const uint64& l, unsigned int n) { auto tmp = l; return tmp <<= n; } MC_END_NAMESPACE

uint64.cpp

#include "uint64.hpp" MC_BEGIN_NAMESPACE uint64::uint64(uint32_t v) { first() = v; second() = 0u; } uint64::uint64(const uint64& o) { IT之家this = o; } uint64& uint64::operator+=(const uint64& o) noexcept { second() += o.second(); // 先计算 second,预防 (this == &o) 的情况 uint32_t old = first(); if ((first() += o.first()) < old) { ++second(); } return IT之家this; } uint64& uint64::operator<<=(unsigned int n) noexcept { if (n < 32) { second() = (second() << n) | (first() >> (32 - n)); first() <<= n; } else if (n < 64) { second() = first() << (n - 32); first() = 0u; } else /IT之家if (n >= 64)IT之家/ { second() = first() = 0u; } return IT之家this; } uint64& uint64::operator>>=(unsigned int n) noexcept { if (n < 32) { first() = (first() >> n) | (second() << (32 - n)); second() >>= n; } else if (n < 64) { first() = second() >> (n - 32); second() = 0u; } else /IT之家if (n >= 64)IT之家/ { second() = first() = 0u; } return IT之家this; } void swap(uint64& l, uint64& r) { if (&l != &r) { auto tmp = l.first(); l.first() = r.first(); r.first() = tmp; tmp = l.second(); l.second() = r.second(); r.second() = tmp; } } MC_END_NAMESPACE

test.cpp

#include <cstdint> #include <cstdio> #include "uint64.hpp" #if 1 typedef mc::uint64 U64; inline void ptype() {std::printf("使用 mc::uint64\n");} #else typedef std::uint64_t U64; inline void ptype() {std::printf("使用 std::uint64_t\n");} #endif void frm(const charIT之家 str) { std::printf("%20s", str); } void data_hex(const U64& v) { const uint8_tIT之家 p = (const uint8_tIT之家)&v; for (int i = 0; i < 8; ++i) { if (i == 4) std::printf(" "); std::printf("%02x", p[i]); } std::printf("\n"); } void test() { uint32_t v = 0xffffffff; U64 a = v; frm("(a = 0xffffffff) => "); data_hex(a); frm("(a >>= 1) => "); data_hex(a >>= 1); a = v; frm("(a <<= 1) => "); data_hex(a <<= 1); a = v; frm("(a += a) => "); data_hex(a += a); } int main() { ptype(); if (mc::maybe_big_endian::value) { std::printf("主机字节序是 big-endian\n"); } else { std::printf("主机字节序是 little-endian\n"); } for (int i = 0; i < 20; ++i) std::printf(" "); if (mc::maybe_big_endian::value) std::printf("H <<<< L H <<<< L\n"); else std::printf("L >>>> H L >>>> H\n"); test(); return 0; }

功能还在逐步完善中,小伙伴们记得关注。

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供学习参考!
本文地址为 https://v30.fanwenzhu.com/jiaob/cjj/8829.shtml

相关文章

风云图片

推荐阅读

返回C/C++频道首页