School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,153 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,672 people online right now. Registration is fast and FREE... Join Now!




Find power of two

 

Find power of two

sam1012

31 Oct, 2009 - 04:02 AM
Post #1

New D.I.C Head
*

Joined: 1 May, 2009
Posts: 21


My Contributions
in this program user will enter a number 0 to 9. and without Mul, Div , Neg, Not I have to find these one : where n is integer

Bx = 2^n

Cx= bx/4

Dx = 1's compliment of Bx


I am a beginner, i want some help. Please just give me just hint to solve my problem.

User is offlineProfile CardPM
+Quote Post


wildgoose

RE: Find Power Of Two

31 Oct, 2009 - 08:46 AM
Post #2

D.I.C Regular
Group Icon

Joined: 29 Jun, 2009
Posts: 430



Thanked: 56 times
Dream Kudos: 25
Expert In: Assembly,C,C++

My Contributions
Sorry, can't do it for you! But your teacher most likely told you how and you missed the boat!

Let's do these things backwards from simplest to most complex!

You're teacher must have taught you that a 1's complement is the inversion (flipping) of the bits.
Asm

0A5h
1010 0101 0A5h (165 unsigned, -165 signed)
0101 1010 05Ah (90)

2's complement is used for a subtraction operation. 1's complement then add 1
Asm

0101 1011 05Bh (91)

So
Asm

0111 1011 07Bh (123)
0001 0111 17h (23) {subtract}
1110 1000 E8h 1's complemenet 17h
1110 1001 E9h 2's complement
now sum
0111 1011 7Bh (123)
1110 1001 E9h {ADD}
--------------
0110 0100 64h ( 100)

WOW! MAGIC!!!!

Okay next problem.
Processors typically have two kinds of shifts though some have four such as the 80x86.

Shift Logical Left
Shift Arithmetic Left
Are the same thing.
Asm

01011010 5Ah (90)
10110100 B4h (180)

The bits get shifted left by N bits. In that case it was by 1 bit. This is effectively a multiply!

Logical Shift Right
Asm

1010 0101 A5 (165)
0101 0010 -> 1 52 (82)

In a Logical Shift Right a zero bit was clocked into the high bit!

For an Arithmetic Shift Right the MSB (Most Significant Bit) the sign bit, acts like a sticky bit. It stays in place and then shifts the data down.
Asm

1010 0101 A5 (165 unsigned -91 signed))
1101 0010 ->1 D2 (210 unsigned -46 signed)

So shift left once is a divide by 2. Shift left twice is a divide by 4.

They cheated here and made this number a base 2.

2^1 = 2
2^2 = 4
2^3 = 8

Left shift????

Also 2^3 = 2*2*2 three 2's.
5^3 = 5*5*5 = 125

Left Shift & Right Shift
Left Rotate & Right Rotate


This post has been edited by wildgoose: 31 Oct, 2009 - 10:21 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 04:57PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month