Is it possible to find (integer) result of divison of a number by 7 by using Bitwise operators only? Please give a method to do so. Thank you.
8 5518
Have a look at "Egyptian division".
JosAH 11,448
Recognized Expert MVP
Have a look at "Egyptian division".
Cute; you can even avoid the multiplication by seven by shifting like this: -
int times_seven(int x) { return (x<<3)-x; }
-
We still do need addition (and subtraction) though ...
kind regards,
Jos
Banfa 9,065
Recognized Expert Moderator Expert
I rather like the method dividing by shift and subtraction
Assuming you are dividing N by 7 then - Divisor D = 7
- Shift D left until the D > N.
- Shift D right 1 place
- IF D >= N { Record a 1, subtract D from N } ELSE { Record a 0 }
- IF D != 7 { GOTO 3 }
- END You record sequence 1s and 0s is the result MSB first and N now contains the remainder
Example -
What is 183 / 7?
-
-
In binary
-
10110111 / 111
-
-
1. D = 111
-
2. Shift divisor D until D > N (10110111): D = 11100000
-
3. Shift D right 1 place: 1110000
-
4. D >= N: Record 1, N -= D: N = 10110111 - 1110000 = 1000111
-
5. D != 111: GOTO 3
-
3. Shift D right 1 place: 111000
-
4. D >= N: Record 1, N -= D: N = 1000111 - 111000 = 1111
-
5. D != 111: GOTO 3
-
3. Shift D right 1 place: 11100
-
4. D < N: Record 0
-
5. D != 111: GOTO 3
-
3. Shift D right 1 place: 1110
-
4. D >= N: Record 1, N -= D: N = 1111 - 1110 = 1
-
5. D != 111: GOTO 3
-
3. Shift D right 1 place: 111
-
4. D < N: Record 0
-
5. D == 111
-
6. Result 11010 N = the remainder = 1
-
-
11010 = 26 so
-
-
183 / 7 = 26 r 1
-
I'm not sure if this is the method already suggested only in different terms or if this method is particularly efficient.
... by using Bitwise operators only? .....
That may well mean comparison, - and + are not allowed.
Banfa 9,065
Recognized Expert Moderator Expert
That may well mean comparison, - and + are not allowed.
How about if you can implement addition through bitwise operators for instance
Adding A plus B
Calculate A' = A xor B
Calculate B' = A and B
If B' == 0 result = A'
else A = A', B = B' << 1 and iterate
Subtraction can be by by adding of a negative value, that is negate the value to subtract (by inverting all bits and adding 1) and then add it to the other value.
It can all be done, basically it is a computer, it holds data as bits so ultimately everything by definition is a bitwise operation :D
JosAH 11,448
Recognized Expert MVP
I'm not sure if this is the method already suggested only in different terms or if this method is particularly efficient.
It's the Egyptian division method; those faraos were clever dickies in their time ;-)
kind regards,
Jos
What do these links have to do with this particular thread?
kind regards,
Jos (moderator)
ps. I'll remove your (and my) reply if my reply isn't answered in a reasonable time span.
This is most definitely spam. I used to moderate the board of a student .org back in college and we would get those sometimes. They post those links so that when the Google bot crawls the forum it increases their page rank.
JosAH 11,448
Recognized Expert MVP
This is most definitely spam. I used to moderate the board of a student .org back in college and we would get those sometimes. They post those links so that when the Google bot crawls the forum it increases their page rank.
Hi, I removed the message and my reply this morning; it is spam alright.
kind regards,
Jos
Sign in to post your reply or Sign up for a free account.
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: Mike Hodkin |
last post by:
As a beginning student of C++, books reference "bitwise operators" and give
brief examples, but I have not read a good explanation of what they are used
for. One reference mentioned that they are used in hardware programming.
Are they used very often in routine C/C++ programming, and what for?
thanks,
MJH
|
by: Tony Johansson |
last post by:
Hello Experts!
Assume I have a class called SphereClass as the base class and a class
called BallClass that is derived from the SphereClass. The copy constructor
initialize the left hand object in this case object b2 below. It also
initialize subobject from class SphereClass.
The BallClass copy constructor looks like this.
BallClass::BallClass(const BallClass& bc) : SphereClass(bc)
|
by: Christopher Benson-Manica |
last post by:
I'm trying to compute the absolute value of an integer using only bitwise
operators...
int x;
sscanf( "%d", &x );
printf( "%d\n", (x^((~((x>>31)&1))+1)) + ((x>>31)&1) );
That works, but it assumes 32 bit integers. Is there a portable/standard way
to do this? Or are ANSI integers always 32 bits? If not, is using
sizeof(int) * 8 - 1 as the shift value an acceptable alternative?
|
by: sandy_pt_in |
last post by:
Hi C guru's
can you please tell me how to do multiplication of 2 numbers using
bitwize operators?? expecting reply.
| |
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: James Radke |
last post by:
Hello,
I found some code that I could use in my application on the web, but it is
written in C#, and I would like to convert it to VB. And I am having
problems with one section. Is there anyone that could tell me how to
convert these two things?
1)
|
by: noridotjabi |
last post by:
I'm learning to program in C and any tutorial or book that I read likes
to briefly touch on birdies operators and then move on without giving
any sort of example application of them. Call me what you will but I
cannot seem to see the purpose for bitwise operators. Especially the
operators bitwise OR ( | ) and bitwise AND ( & ), I'm just not getting
it. I have searched around and really haven't found anything that gave
explanation to why...
|
by: Mark Rae |
last post by:
Hi,
This isn't *specifically* an ASP.NET question, so I've also posted it in the
ADO.NET group - however, it's not too far off-topic...
Imagine a SQL Server 2005 database with a table with an int column used for
bitwise data - you know the sort of thing... 0, 1, 2, 4, 8, 16 etc
intBitwise strName
----------------------
|
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: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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: 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: 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: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |