I want an algorithm that do arithmetic operations(divi de,mutiply,add
etc.)just using bitwise operators:<<,>> ,&,|,^;
For example,how "a/10" can be implemented.
I just want a hint.
Thanks. 7 4507
In article <11************ *********@l41g2 000cwc.googlegr oups.com>,
Jerry <zi****@gmail.c om> wrote:
:I want an algorithm that do arithmetic operations(divi de,mutiply,add
:etc.)just using bitwise operators:<<,>> ,&,|,^;
:For example,how "a/10" can be implemented.
:I just want a hint.
Think "long division".
--
Beware of bugs in the above code; I have only proved it correct,
not tried it. -- Donald Knuth
Jerry wrote: I want an algorithm that do arithmetic operations(divi de,mutiply,add etc.)just using bitwise operators:<<,>> ,&,|,^;
For example,how "a/10" can be implemented.
Oddly enough, just the other day I figured out how to do increment or
decrement by a value 2^n. With enough fiddling you could do arbitrary
addition and subtraction by any value (2's complement if you want to
use for signed values.) Some more fiddling would allow you to use 100
byte or 200 byte values--though you could do the same using + and -
creatively.
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#define INT_BIT (CHAR_BIT * sizeof(int))
int main(void) {
int L, leftShift, i, changeMask;
leftShift = 0; /* added or subtracted value will be 2^leftShift */
i = 25; /* value we are adding to or subtracting from */
changeMask = 1 << leftShift;
for (L = leftShift; L < INT_BIT; L++) {
i ^= changeMask;
if ( /* ! */ (i & changeMask)) { /* comment in or out "!" for
addition or subtraction */
break;
}
changeMask <<= 1;
}
printf("%i", i);
return 0;
}
Knowing that this would work came about from fiddling with a binary
tree (...it would take a bit to explain beyond that.) Though I am not
sure there is any point in creating such code--I do believe that
processors boil down mathematical operations to logical operations, but
I would have to assume intel has a better algorithm than this.
-Chris
Jerry wrote: I want an algorithm that do arithmetic operations (divide, mutiply, add etc.) just using bitwise operators:<<,>> ,&,|,^;
For example,how "a/10" can be implemented.
I just want a hint.
<http://cbfalconer.home .att.net/download/dubldabl.txt>
--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
"Jerry" <zi****@gmail.c om> writes: I want an algorithm that do arithmetic operations(divi de,mutiply,add etc.)just using bitwise operators:<<,>> ,&,|,^; For example,how "a/10" can be implemented.
I just want a hint.
Um, why do you want to do this? My first thought is that if you want
to do division, just use the "/" operator; that's what it's there for.
I'm not implying that you shouldn't do this, but we can probably be
more helpful if we understand the rationale. It can also be useful in
nailing down the requirements; did you mean to exclude the unary "~"
operator, or was that just an oversight?
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
The situation is such:
We are processing a project porting products on Windows platform to
Mac. OS.,and,we are not familar with Mac.so there are always some
troublesome things bother us.
Today my partner want a QWORD data type and want to use 'sturct' to
difine QWORD variables,and,d o artithmetics operations with such
variables.
I remember that there are methods that can do this just using BitwiSe
operation.
If popssible,it should be convenient.
That's the orignal of all.
Thanks again.^_~
"Jerry" <zi****@gmail.c om> writes: The situation is such: We are processing a project porting products on Windows platform to Mac. OS.,and,we are not familar with Mac.so there are always some troublesome things bother us. Today my partner want a QWORD data type and want to use 'sturct' to difine QWORD variables,and,d o artithmetics operations with such variables. I remember that there are methods that can do this just using BitwiSe operation.
I'm not sure how big a QWORD is, but if you want to implement, say,
128-bit arithmetic on a system that only supports 64-bit arithmetic,
bitwise operators are not the best approach. For addition, for
example, it's going to be a lot easier to use addition on the lower
and upper halves with a little extra code to handle carries. The
technique is well known (but I don't know the details).
I'm sure it's possible using just bitwise operators, but it's going to
be slow, difficult, and error-prone.
(If a QWORD is 64 bits, there's a good chance your compiler supports
it directly, probably as "long long".)
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Keith Thompson wrote: I'm not sure how big a QWORD is, but if you want to implement, say, 128-bit arithmetic on a system that only supports 64-bit arithmetic, bitwise operators are not the best approach. For addition, for example, it's going to be a lot easier to use addition on the lower and upper halves with a little extra code to handle carries. The technique is well known (but I don't know the details).
I'm sure it's possible using just bitwise operators, but it's going
to be slow, difficult, and error-prone.
(If a QWORD is 64 bits, there's a good chance your compiler supports it directly, probably as "long long".)
A QWORD is 64 and DWORD 32.
Doing a search for "64-bit mac apple c++" (c++ so it will be less
likely to be ignored), it appears that there are probably "long long"
and "unsigned long long" in the Mac world.
Looking at my Windows header files, it looks like you will want to try:
typedef unsigned long long QWORD;
And that would be that.
-Chris This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Randell D. |
last post by:
Why would one use bitwise operators? I can program in various languages in
some shape or form (C++, PHP, some scripting) and I've heard/seen bitwise
operators before, but never understood why anyone would use them - any real
world examples or ideas? Examples follow (that I am reading in my Core
JavaScript Guide 1.5).
15 & 9 yields 9 (1111 & 1001 = 1001)
15 | 9 yields 15 (1111 | 1001 = 1111)
15 ^ 9 yields 6 (1111 ^ 1001 = 0110)
in...
|
by: sandy_pt_in |
last post by:
How to mulitply two integer numbers using bitwise operators in C
language.Please reply as early as possible
|
by: Serve Laurijssen |
last post by:
Some people prefer to use
"if (x & 1)"
to see if a number is odd or even. Is this completely portable according to
the standard?
|
by: Steve Summit |
last post by:
-----BEGIN PGP SIGNED MESSAGE-----
It's often explained that the reason for some of the imprecision
in C's definition is so that C can be implemented on different
kinds of machines -- say, those with 2's complement,
1's complement, or sign-magnitude arithmetic. But the followup
remark is sometimes also made that the choice of arithmetic isn't
completely unconstrained, since the bitwise operators seem to
presume a base-2 machine.
|
by: Paul E Collins |
last post by:
Suppose I have a few Keys objects:
Keys k1 = Keys.V; // V
Keys k2 = Keys.Control | Keys.V; // Ctrl+V
Keys k3 = Keys.Shift | Keys.J; // Shift+J
I need to determine which of these include the Keys.V element,
regardless of any other keys. I know it will be a bitwise comparison,
but I can't work out the correct syntax to use.
| |
by: David R. |
last post by:
I want to do bitwise operation on some large integers.
For example,
Response.Write CBool(2 AND 2^30) ' returns False
Response.Write CBool(2 AND 2^31) ' CRASHED!
Looks like the AND operator can only deal with integers up to 2147483647?
Is there a way to make the AND operator work with larger integers, such as
the double datatype?
|
by: Carramba |
last post by:
Hi!
I now that I can't do straight forward any bitwise operation on float
(double etc..). But I wondering what is the easiest/best way to do this?
I was thinking if I have float x=1.1111 so I can multiple it by 1000 to
get 11111 and the preform bitwise like <<2 to get 88888 and then divide
by 1000 to go back to float 8.8888. but these seem like "nasty" way to
do it. So maybe some of you have great tips?
Thank you in advance!
L R
|
by: Carl Banks |
last post by:
Anyone with me here? (I know the deadline for P3 PEPs has passed; this
is just talk.)
Not many people are bit-fiddling these days. One of the main uses of bit
fields is flags, but that's not often done in Python because of keyword
arguments and dicts, which are lot more versatile. Another major use,
talking to hardware, is not something oft done in Python either.
It seems like this occasional usage wouldn't justify having built-in...
|
by: Santhosh |
last post by:
Hi to all,
How the individual digits of a number can be obtained using the
bitwise operators alone.Is it possible to do it ?
If we have n = 34
Result has to be 3,4.
Thanks a billion for your reply in advance.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |