Website Summary Files Commits Branches @commit @diff @stats
path: root / pract_KN / pract. 07 / solutions.cpp
diff options
context:
space:
mode:
Commitaacdf322834cceaf4eca08fc9cf24db7457616ff (patch)
Author
 Stoycho Kyosev <68247567+stoychoX@users.noreply.github.com>
2021-11-24 23:49:09 +0200
Committer
 GitHub <noreply@github.com>
2021-11-24 23:49:09 +0200
Files867d1e4fa779091731bc9d249cded11c78460078 /pract_KN/pract. 07/solutions.cpp
Introduction_to_programming_FMI-aacdf322834cceaf4eca08fc9cf24db7457616ff.tar
Introduction_to_programming_FMI-aacdf322834cceaf4eca08fc9cf24db7457616ff.tar.gz
Introduction_to_programming_FMI-aacdf322834cceaf4eca08fc9cf24db7457616ff.zip
Parent7c70a500a2e9316f66321e70d8d523723cd8176c (diff)
Add files via upload
1 files changed, 51 insertions, 0 deletions
-rw-r--r--pract_KN/pract. 07/solutions.cpp51
diff --git a/pract_KN/pract. 07/solutions.cpp b/pract_KN/pract. 07/solutions.cpp
new file mode 100644
index 0000000..f53555c
--- /dev/null
+++ b/pract_KN/pract. 07/solutions.cpp
@@ -0,0 +1,51 @@
+void getBitwiseRepresentation(int n) {
+ for (int i = 31; i >= 0; --i)
+ std::cout << ((n & (1 << i)) != 0);
+}
+
+void flipFourthBit(int& n) {
+ int mask = 1 << 3;
+ n = n ^ mask;
+}
+
+int numberOfZeroesInBinaryRepresentation(int n) {
+ int cnt = 0;
+ for (int i = 0; i < 32; i++)
+ cnt += ((n & (1 << i)) == 0);
+
+ return cnt;
+}
+
+void setBit(int& n, int bitPosition, bool newBitValue) {
+ int mask = 1 << bitPosition;
+ bool oldBitIsOne = ((mask & n) != 0);
+ if (oldBitIsOne ^ newBitValue) n = n ^ mask;
+}
+
+void printSubset(int arr[], int length, int indexOfSet) {
+ std::cout << "[";
+ int commas = 0;
+ int numberOfOnes = 32 - numberOfZeroesInBinaryRepresentation(indexOfSet);
+
+ for (int i = 0; i < length; i++) {
+ if (((indexOfSet & (1 << i)) != 0)) {
+ std::cout << arr[i];
+
+ if (++commas < numberOfOnes)
+ std::cout << ", ";
+ }
+ }
+ std::cout << ']' << std::endl;
+}
+
+void generateSubSets(int arr[], int length) {
+ int mask = 0;
+ int border = 1 << length;
+ for (int i = 0; i < border; i++)
+ printSubset(arr, length, i);
+}
+
+//bonus: function to change sign of a number
+void changeSign(int& n) {
+ n = ~n + 1;
+}