Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How do i find the greatest power of 2 without using division using - Right shift

ID: 3650467 • Letter: H

Question

How do i find the greatest power of 2 without using division using - Right shifting an unsigned number

Explanation / Answer

Here is a quick-rundown of the arithmetic instruction format, when writing your SPARC code: mnemonic %rsrcA, srcB, %rdest The instruction mnemonic (commonly, but inaccurately, also referred to as the "opcode") specifies the type of operation to be preformed. rsrcA is the first operand, while srcB is the second. rsrcA must be a register, and srcB can usually be a signed immediate (13 bit) constant or a second register. rdest is the destination register, and not all instructions have one (for example: cmp for compare). Note that in SPARC assembly language, instructions always read from left to right. The left (and middle, if present) operands are source operands. If the operation writes a result anywhere (as almost all operations do), the result is written to the rightmost operand (destination register or destination memory location). In SPARC assembly language, the following instructions are completely valid: add %r3, %r4, %r5 add %r3, 16, %r5 The first instruction adds register 3 and register 4, and places the result in register 5. The second instruction adds register 3, and the number 16, and places the result in register 5. def power_of_2?(number) return false if number == 0 while(number % 2 == 0) number = number / 2 end return false if number > 1 true end def power_of_2?(number) return true if number == 1 return false if number == 0 || number % 2 != 0 power_of_2?(number / 2) end Once again, it solves the problem, but this time recursively. Since those early days of Java and C, I’ve played around with a whole bunch of languages, some just as imperative, but others were functional or had functional capabilities. In such languages, recursion is the norm or at least a lot more prevalent. I find that I really like recursive solutions, it is often a very elegant way to get around some ugly code. Even though it is not the first stop my brain makes when it comes to solving programming problems, I find that these days I almost always consider if there might be a recursive way to approach a problem. I don’t yet always instinctively feel if a problem has a recursive answer, but in many cases (like this one) it is fairly simple. As long as you can divide the initial problem into two smaller parts which are “equivalent” to the original problem, you can solve it recursively (like when you split a tree you get two smaller trees). I do wonder if programmers who were “weaned” on functional languages would go for the recursive solution first and if they find one do they bother to consider an iterative one at all? If you’re such a developer, please share how you think.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote