Write 2 functions using iterations:
* while loop: While loops are iterating if statements while a condition is true keep looping* for loop: For loops have a known start, stop and increment/decrement value to keep looping
These functions determine the binary bit-pattern of a decimal number. There are a number of bitwise operators that can be used to determine the bit-pattern. See Bitwise.py for examples of using bitwise operators. Here’s a general algorithm that can be used (that doesn’t use looping):
Algorithm Bitpattern(number)1 if number & (1 << 0) > 0 # check first bit2 print(1)3 else4 print(0)5 if number & (1 << 1) > 0 # check second bit6 print(1)7 else8 print(0) 9 if number & (1 << 2) > 0 # check third bit10 print(1)11 else12 print(0) 13 “¦ etc.
The problem will be when to stop shifting while looping. A way to determine the number of shifts can be found using logarithms:
Algorithm ShiftBits(number) log10 = math.log(number+1) log2 = log10 / math.log(2) shifts = math.ceil(log2) return shifts
Note: To use the log or ceil function:
1. First: import math 2. To call the ceil or log function: math.log(number) or math.ceil(number)
Consequently, to determine the number of iterations for a FOR loop using decimal 10 is 4. Here’s proof:
2**0 = 12**1 = 22**2 = 42**3 = 8
Decimal(10) = 1(2**3[8]) + 0(2**2[4]) + 1(2**1[2]) + 0(2**0)[1]) = 1010 binary
For Loops Python Programming Discussion
Our Service Charter
1. Professional & Expert Writers: Blackboard Experts only hires the best. Our writers are specially selected and recruited, after which they undergo further training to perfect their skills for specialization purposes. Moreover, our writers are holders of masters and Ph.D. degrees. They have impressive academic records, besides being native English speakers.
2. Top Quality Papers: Our customers are always guaranteed of papers that exceed their expectations. All our writers have +5 years of experience. This implies that all papers are written by individuals who are experts in their fields. In addition, the quality team reviews all the papers before sending them to the customers.
3. Plagiarism-Free Papers: All papers provided by Blackboard Experts are written from scratch. Appropriate referencing and citation of key information are followed. Plagiarism checkers are used by the Quality assurance team and our editors just to double-check that there are no instances of plagiarism.
4. Timely Delivery: Time wasted is equivalent to a failed dedication and commitment. Blackboard Experts is known for timely delivery of any pending customer orders. Customers are well informed of the progress of their papers to ensure they keep track of what the writer is providing before the final draft is sent for grading.
5. Affordable Prices: Our prices are fairly structured to fit in all groups. Any customer willing to place their assignments with us can do so at very affordable prices. In addition, our customers enjoy regular discounts and bonuses.
6. 24/7 Customer Support: At Blackboard Experts, we have put in place a team of experts who answer to all customer inquiries promptly. The best part is the ever-availability of the team. Customers can make inquiries anytime.
