473,659 Members | 3,592 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Division by 7 without using the divide operator

Hello everyone,

I'm not really new to programming but my bitwise skills definately need
work.
I came across a problem I've been trying to figure out for fun. I'm
trying to write
a routine that will divide a number by 7 without using the divide
operator. I
figured how to multiply by 7 using bitwise operators ( (x << 3) - x )
but I having
a hard time trying to figure a bitwise approach to do the divide. The
closed I've
been able to come is:

((x>3) + x) >3

This works for most cases but not all. Am I approaching this problem
wrong?
is it possible to divide by 7 using bitwise operators? Any advice or
help would be
greatly appreciated.

Thanks
justin

Jan 19 '07 #1
1 4601
* Ju************* *@gmail.com:
Hello everyone,

I'm not really new to programming but my bitwise skills definately need
work.
I came across a problem I've been trying to figure out for fun. I'm
trying to write
a routine that will divide a number by 7 without using the divide
operator. I
figured how to multiply by 7 using bitwise operators ( (x << 3) - x )
but I having
a hard time trying to figure a bitwise approach to do the divide. The
closed I've
been able to come is:

((x>3) + x) >3

This works for most cases but not all. Am I approaching this problem
wrong?
is it possible to divide by 7 using bitwise operators? Any advice or
help would be
greatly appreciated.
Your draft solution can be approximated as (x/8+x)/8 = (9x/8)/8=(9/64)x.
Since 9 decimal = 1001 binary, and since 1/64 = 0.000001 binary, that
is essentially the same as multiplying x by 1001*0.000001 = 0.001001
binary, wheras 1/7 = 0.0010010010010 010... binary. Perhaps you can see
a pattern both in that expansion and in your first draft solution?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jan 19 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
12742
by: Marcin | last post by:
How I can make division of two numbers placed in arrays, example: short int a = {2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2}; short int b = {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2}; short int result = a / b; 'division without additional lib's and header files, only in standart C.
17
2411
by: seb.haase | last post by:
Hi, Is it true that that "Python 3000" is dead ? Honestly I think that e.g. changing 5/2 to be 2.5 (instead of 2) would just break to much code :-( On the otherhand I'm using Python as "Matlab replacement" and would generally like 5/2 ==2.5 So, I was contemplating to default all my modules/scripts to start with "from __future__ import division" but if it is never coming (in this decade, that is) then it would be a
13
12423
by: Sri | last post by:
Hi, Is there anyway I can implement division in C without using the '/' operator? Can I use bit aritmetic or such? Thanks, Sri
10
3175
by: Mike S | last post by:
Does anyone know the logic behind why in VB.NET the result of a floating-point division ('/') is -rounded- on being converted to an integer type, such as with statements like Dim x As Integer = 2/3 'after assignment, x is 1, whereas a sane person would say it should be 0 Does Microsoft have a reason for this design decision? I understand that this type of rounding can reduce the overall error in long computation chains by reducing the...
31
2544
by: krypto.wizard | last post by:
How to divide a number by 7 efficiently without using - or / operator. We can use the bit operators. I was thinking about bit shift operator but I don't know the correct answer.
94
11459
by: krypto.wizard | last post by:
Last month I appeared for an interview with EA sports and they asked me this question. How would you divide a number by 7 without using division operator ? I did by doing a subtraction and keeping a counter that kept a tab on how many times I subtracted. Later, the EA sport guy told me that of course there are can be better technique by using bit operator.
1
3712
by: youjay | last post by:
I've been out of perl for a while, so I am starting from scratch. I have a small applet which scans a set of directories, getting information from some files in each one, and displaying selected data. The script works good, but I need to set in some tabbing on the terminal, and that is where the problem comes in. as soon as I attempt a division, the applet blows up, and it is not a divide by 0 error. I have tested the particular line of...
16
5280
by: spl | last post by:
To increase the performance, how to change the / operator with bitwise operators? for ex: 25/5, 225/25 or 25/3 or any division, but I am not bothered of any remainder.
0
8337
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
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...
0
8748
jinu1996
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...
1
6181
isladogs
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...
0
5650
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();...
0
4175
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...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1978
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.