473,508 Members | 2,053 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 4585
* 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.0010010010010010... 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
12731
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 =...
17
2392
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...
13
12406
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
3161
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 =...
31
2527
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
11401
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...
1
3705
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...
16
5252
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
7125
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...
0
7328
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,...
1
7049
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...
1
5055
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...
0
4709
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...
0
3199
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...
0
1561
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
767
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
422
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...

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.