Skip to content

Commit ee5a284

Browse files
Tyler WhitneyTyler Whitney
authored andcommitted
extend rotl,rotr to show negative rotation
1 parent 63bb6c1 commit ee5a284

2 files changed

Lines changed: 33 additions & 25 deletions

File tree

docs/standard-library/bit-functions.md

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "<bit> functions"
33
description: "Functions to access, manipulate, and process individual bits and sequences of bits"
4-
ms.date: "08/25/2020"
4+
ms.date: "08/28/2020"
55
f1_keywords: ["bit/std::bit_cast", "bit/std::has_single_bit", "bit/std::bit_ceil", "bit/std::bit_floor", "bit/std::bit_width", "bit/std::rotl", "bit/std::rotr", "bit/std::countl_zero", "bit/std::countl_one","bit/std::countr_zero","bit/std::countr_one","bit/std::popcount"]
66
helpviewer_keywords: ["std::bit [C++], bit_cast", "std::bit [C++], has_single_bit", "std::bit [C++], bit_ceil", "std::bit [C++], bit_floor", "std::bit [C++], bit_width", "std::bit [C++], rotl", "std::bit [C++], rotr", "std::bit [C++], countl_zero", "std::bit [C++], countl_one", "std::bit [C++], countr_zero", "std::bit [C++], countr_one", "std::bit [C++], popcount"]
77
---
@@ -11,18 +11,18 @@ The \<bit> header includes the following non-member template functions:
1111

1212
| **Non-member functions** | **Description** |
1313
|--------|---------|
14-
|[bit_cast](#bit_cast) | Reinterpret the object representation of one type to another. |
14+
|[bit_cast](#bit_cast) | Reinterpret the object representation from one type to another. |
1515
|[bit_ceil](#bit_ceil) | Find the smallest power of two greater than or equal to a value. |
1616
|[bit_floor](#bit_floor) | Find the largest power of two not greater than a value. |
1717
|[bit_width](#bit_width) | Find the smallest number of bits needed to represent a value. |
1818
|[countl_zero](#countl_zero) | Count the number of consecutive bits set to zero, starting from the most significant bit. |
1919
|[countl_one](#countl_one) | Count the number of consecutive bits set to one, starting from the most significant bit. |
2020
|[countr_zero](#countr_zero) | Count the number of consecutive bits set to zero, starting from the least significant bit. |
2121
|[countr_one](#countl_one) | Count the number of consecutive bits set to one, starting from the least significant bit. |
22-
|[has_single_bit](#has_single_bit) | Check if a value has only one bit set (which means it is a power of two). |
23-
|[popcount](#popcount) | Count the number of bits set to one in an unsigned integer. |
24-
|[rotl](#rotl) | Compute the result of bitwise left rotation. |
25-
|[rotr](#rotr) | Compute the result of bitwise right rotation. |
22+
|[has_single_bit](#has_single_bit) | Check if a value has only one bit set. This is the same as testing whether a value is a power of two. |
23+
|[popcount](#popcount) | Count the number of bits set to one. |
24+
|[rotl](#rotl) | Compute the result of a bitwise left rotation. |
25+
|[rotr](#rotr) | Compute the result of a bitwise right rotation. |
2626

2727
## <a name="bit_cast"></a>`bit_cast`
2828

@@ -73,11 +73,11 @@ std::bit_cat<int>(f) = 7f800000
7373

7474
### Remarks
7575

76-
Low-level code often needs to interpret an object of one type as another type. The reinterpreted object has the same bit representation as the original, but is a different type. But making the bit-for-bit conversion can be error-prone.
76+
Low-level code often needs to interpret an object of one type as another type. The reinterpreted object has the same bit representation as the original, but is a different type.
7777

7878
Instead of using `reinterpret_cast`, or `memcpy()`, `bit_cast()` is a better way to make these conversions. It's better because:
7979
- `bit_cast()` is `constexpr`
80-
- `bit_cast()` requires the types to be trivially copyable and the same size. This prevents potential problems with using `reinterpret_cast` and `memcpy` because they could be used to inadvertently, and incorrectly, convert non-trivially-copyable types. Also, `memcpy()` could be used to inadvertently copy between types that aren't the same size. For example, a double (8 bytes) into an unsigned int (4 bytes), or the other way around.
80+
- `bit_cast()` requires the types to be trivially copyable and the same size. This prevents potential problems that you could encounter using `reinterpret_cast` and `memcpy` because they could be used to inadvertently, and incorrectly, convert non-trivially-copyable types. Also, `memcpy()` could be used to inadvertently copy between types that aren't the same size. For example, a double (8 bytes) into an unsigned int (4 bytes), or the other way around.
8181

8282
This overload only participates in overload resolution if:
8383
- `sizeof(To) == sizeof(From)`
@@ -208,7 +208,7 @@ The unsigned integer value to test.
208208
209209
### Return Value
210210
211-
The number of bits needed to store `value`.
211+
The number of bits needed to represent `value`.
212212
213213
If `value` is zero, returns zero.
214214
@@ -458,7 +458,7 @@ This template function only participates in overload resolution if `T` is an uns
458458

459459
## <a name="has_single_bit"></a>`has_single_bit`
460460

461-
Check if a value has only one bit set (which means it is a power of two).
461+
Check if a value has only one bit set.This is the same as testing whether a value is a power of two.
462462

463463
```cpp
464464
template <class T>
@@ -472,7 +472,7 @@ The unsigned integer value to test.
472472
473473
### Return value
474474
475-
`true` if `value` has only one bit set (which means it is a power of two). Otherwise, `false`.
475+
`true` if `value` has only one bit set which also means that `value` is a power of two. Otherwise, `false`.
476476
477477
### Example
478478
@@ -512,7 +512,7 @@ This template function only participates in overload resolution if `T` is an uns
512512

513513
## <a name="popcount"></a>`popcount`
514514

515-
Count the number of set bits in an unsigned integer value.
515+
Count the number of bits set to one in an unsigned integer value.
516516

517517
```cpp
518518
template<class T>
@@ -526,7 +526,7 @@ The unsigned integer value to test.
526526
527527
### Return value
528528
529-
The number of set bits in `value`.
529+
The number bits set to one in `value`.
530530
531531
### Example
532532
@@ -584,7 +584,7 @@ nodiscard constexpr T rotl(T value, int s) noexcept;
584584
The unsigned integer value to rotate.
585585
586586
*s*\
587-
The number of left rotations.
587+
The number of left rotations to perform.
588588
589589
### Return value
590590
@@ -608,6 +608,9 @@ int main()
608608
bits = std::rotl(bits, 1);
609609
std::cout << std::bitset<8>(bits) << '\n';
610610
}
611+
std::cout << "rotl(" << std::bitset<8>(bits) << ",-1) = ";
612+
bits = std::rotl(bits, -1);
613+
std::cout << std::bitset<8>(bits);
611614
return 0;
612615
}
613616
```
@@ -621,6 +624,7 @@ rotl(00010000, 1) = 00100000
621624
rotl(00100000, 1) = 01000000
622625
rotl(01000000, 1) = 10000000
623626
rotl(10000000, 1) = 00000001
627+
rotl(00000001,-1) = 10000000
624628
```
625629

626630
### Remarks
@@ -642,7 +646,7 @@ nodiscard constexpr T rotr(T value, int s) noexcept;
642646
The unsigned integer value to rotate.
643647
644648
*s*\
645-
The number of right rotations.
649+
The number of right rotations to perform.
646650
647651
### Return value
648652
@@ -666,6 +670,9 @@ int main()
666670
bits = std::rotr(bits, 1);
667671
std::cout << std::bitset<8>(bits) << '\n';
668672
}
673+
std::cout << "rotr(" << std::bitset<8>(bits) << ",-1) = ";
674+
bits = std::rotr(bits, -1);
675+
std::cout << std::bitset<8>(bits);
669676
return 0;
670677
}
671678
```
@@ -679,6 +686,7 @@ rotr(00001000, 1) = 00000100
679686
rotr(00000100, 1) = 00000010
680687
rotr(00000010, 1) = 00000001
681688
rotr(00000001, 1) = 10000000
689+
rotr(10000000,-1) = 00000001
682690
```
683691

684692
### Remarks

docs/standard-library/bit.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "&lt;bit&gt;"
33
description: "Functions to access, manipulate, and process individual bits and sequences of bits."
4-
ms.date: "08/27/2020"
4+
ms.date: "08/28/2020"
55
f1_keywords: ["<bit>"]
66
helpviewer_keywords: ["bit header"]
77
---
@@ -32,18 +32,18 @@ For example, there are functions to rotate bits, find the number of consecutive
3232

3333
| Function | Description |
3434
|-----|-----|
35-
|[bit_cast](bit-functions.md#bit_cast) | Reinterpret the object representation of one type to another. |
36-
|[bit_ceil](bit-functions.md#bit_ceil) | Find the smallest power of two greater than or equal to the given value. |
37-
|[bit_floor](bit-functions.md#bit_floor) | Find the largest integral power of two not greater than the given value. |
38-
|[bit_width](bit-functions.md#bit_width) | Find the smallest number of bits needed to represent the given value. |
39-
|[countl_zero](bit-functions.md#countl_zero) | Count the number of consecutive zero bits, starting from the most significant bit. |
35+
|[bit_cast](bit-functions.md#bit_cast) | Reinterpret the object representation from one type to another. |
36+
|[bit_ceil](bit-functions.md#bit_ceil) | Find the smallest power of two greater than or equal to a value. |
37+
|[bit_floor](bit-functions.md#bit_floor) | Find the largest integral power of two not greater than a value. |
38+
|[bit_width](bit-functions.md#bit_width) | Find the smallest number of bits needed to represent a value. |
39+
|[countl_zero](bit-functions.md#countl_zero) | Count the number of consecutive bits set to zero, starting from the most significant bit. |
4040
|[countl_one](bit-functions.md#countl_one) | Count the number of consecutive bits set to one, starting from the most significant bit. |
4141
|[countr_zero](bit-functions.md#countr_zero) | Count the number of consecutive bits set to zero, starting from the least significant bit. |
4242
|[countr_one](bit-functions.md#countl_one) | Count the number of consecutive bits set to one, starting from the least significant bit. |
43-
|[has_single_bit](bit-functions.md#has_single_bit) | Check if a value has only a single bit set; that is, if the value is an integral power of two. |
44-
|[popcount](bit-functions.md#popcount) | Count the number of bits set to one in an unsigned integer. |
45-
|[rotl](bit-functions.md#rotl) | Compute the result of bitwise left-rotation. |
46-
|[rotr](bit-functions.md#rotr) | Compute the result of bitwise right-rotation. |
43+
|[has_single_bit](bit-functions.md#has_single_bit) | Check if a value has only one bit set. This is the same as testing whether a value is a power of two. |
44+
|[popcount](bit-functions.md#popcount) | Count the number of bits set to one. |
45+
|[rotl](bit-functions.md#rotl) | Compute the result of a bitwise left-rotation. |
46+
|[rotr](bit-functions.md#rotr) | Compute the result of a bitwise right-rotation. |
4747

4848
## See also
4949

0 commit comments

Comments
 (0)