Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
24fb4b76
R
Rust
项目概览
int
/
Rust
接近 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
24fb4b76
编写于
2月 27, 2018
作者:
A
Amanieu d'Antras
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add reverse_bits to integer types
上级
02e021b6
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
66 addition
and
0 deletion
+66
-0
src/libcore/num/mod.rs
src/libcore/num/mod.rs
+54
-0
src/libcore/tests/lib.rs
src/libcore/tests/lib.rs
+1
-0
src/libcore/tests/num/uint_macros.rs
src/libcore/tests/num/uint_macros.rs
+11
-0
未找到文件。
src/libcore/num/mod.rs
浏览文件 @
24fb4b76
...
...
@@ -321,6 +321,33 @@ pub fn swap_bytes(self) -> Self {
(
self
as
$UnsignedT
)
.swap_bytes
()
as
Self
}
/// Reverses the bit pattern of the integer.
///
/// # Examples
///
/// Please note that this example is shared between integer types.
/// Which explains why `i16` is used here.
///
/// Basic usage:
///
/// ```
/// #![feature(reverse_bits)]
///
/// let n: i16 = 0b0000000_01010101;
/// assert_eq!(n, 85);
///
/// let m = n.reverse_bits();
///
/// assert_eq!(m as u16, 0b10101010_00000000);
/// assert_eq!(m, -22016);
/// ```
#[unstable(feature
=
"reverse_bits"
,
issue
=
"48763"
)]
#[cfg(not(stage0))]
#[inline]
pub
fn
reverse_bits
(
self
)
->
Self
{
(
self
as
$UnsignedT
)
.reverse_bits
()
as
Self
}
doc_comment!
{
concat!
(
"Converts an integer from big endian to the target's endianness.
...
...
@@ -1773,6 +1800,33 @@ pub fn swap_bytes(self) -> Self {
unsafe
{
intrinsics
::
bswap
(
self
as
$ActualT
)
as
Self
}
}
/// Reverses the bit pattern of the integer.
///
/// # Examples
///
/// Basic usage:
///
/// Please note that this example is shared between integer types.
/// Which explains why `u16` is used here.
///
/// ```
/// #![feature(reverse_bits)]
///
/// let n: u16 = 0b0000000_01010101;
/// assert_eq!(n, 85);
///
/// let m = n.reverse_bits();
///
/// assert_eq!(m, 0b10101010_00000000);
/// assert_eq!(m, 43520);
/// ```
#[unstable(feature
=
"reverse_bits"
,
issue
=
"48763"
)]
#[cfg(not(stage0))]
#[inline]
pub
fn
reverse_bits
(
self
)
->
Self
{
unsafe
{
intrinsics
::
bitreverse
(
self
as
$ActualT
)
as
Self
}
}
doc_comment!
{
concat!
(
"Converts an integer from big endian to the target's endianness.
...
...
src/libcore/tests/lib.rs
浏览文件 @
24fb4b76
...
...
@@ -46,6 +46,7 @@
#![feature(try_trait)]
#![feature(exact_chunks)]
#![feature(atomic_nand)]
#![feature(reverse_bits)]
extern
crate
core
;
extern
crate
test
;
...
...
src/libcore/tests/num/uint_macros.rs
浏览文件 @
24fb4b76
...
...
@@ -97,6 +97,17 @@ fn test_swap_bytes() {
assert_eq!
(
_1
.swap_bytes
(),
_1
);
}
#[test]
fn
test_reverse_bits
()
{
assert_eq!
(
A
.reverse_bits
()
.reverse_bits
(),
A
);
assert_eq!
(
B
.reverse_bits
()
.reverse_bits
(),
B
);
assert_eq!
(
C
.reverse_bits
()
.reverse_bits
(),
C
);
// Swapping these should make no difference
assert_eq!
(
_0
.reverse_bits
(),
_0
);
assert_eq!
(
_1
.reverse_bits
(),
_1
);
}
#[test]
fn
test_le
()
{
assert_eq!
(
$T
::
from_le
(
A
.to_le
()),
A
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录