473,785 Members | 2,457 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Divison by 7 using bitwise operators only.

32 New Member
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.
Oct 20 '08 #1
8 5518
arnaudk
424 Contributor
Have a look at "Egyptian division".
Oct 20 '08 #2
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:

Expand|Select|Wrap|Line Numbers
  1. int times_seven(int x) { return (x<<3)-x; }
  2.  
We still do need addition (and subtraction) though ...

kind regards,

Jos
Oct 21 '08 #3
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
  1. Divisor D = 7
  2. Shift D left until the D > N.
  3. Shift D right 1 place
  4. IF D >= N { Record a 1, subtract D from N } ELSE { Record a 0 }
  5. IF D != 7 { GOTO 3 }
  6. END You record sequence 1s and 0s is the result MSB first and N now contains the remainder

Example
Expand|Select|Wrap|Line Numbers
  1. What is 183 / 7?
  2.  
  3. In binary
  4. 10110111 / 111
  5.  
  6. 1. D = 111
  7. 2. Shift divisor D until D > N (10110111): D = 11100000
  8. 3. Shift D right 1 place: 1110000
  9. 4. D >= N: Record 1, N -= D: N = 10110111 - 1110000 = 1000111
  10. 5. D != 111: GOTO 3
  11. 3. Shift D right 1 place: 111000
  12. 4. D >= N: Record 1, N -= D: N = 1000111 - 111000 = 1111
  13. 5. D != 111: GOTO 3
  14. 3. Shift D right 1 place: 11100
  15. 4. D < N: Record 0
  16. 5. D != 111: GOTO 3
  17. 3. Shift D right 1 place: 1110
  18. 4. D >= N: Record 1, N -= D: N = 1111 - 1110 = 1
  19. 5. D != 111: GOTO 3
  20. 3. Shift D right 1 place: 111
  21. 4. D < N: Record 0
  22. 5. D == 111
  23. 6. Result 11010 N = the remainder = 1
  24.  
  25. 11010 = 26 so
  26.  
  27. 183 / 7 = 26 r 1
  28.  
I'm not sure if this is the method already suggested only in different terms or if this method is particularly efficient.
Oct 21 '08 #4
r035198x
13,262 MVP
... by using Bitwise operators only? .....
That may well mean comparison, - and + are not allowed.
Oct 21 '08 #5
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
Oct 21 '08 #6
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
Oct 21 '08 #7
Pragma
1 New Member
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.
Oct 21 '08 #8
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
Oct 22 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

11
9028
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...
4
2490
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
14
2360
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)
34
16881
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?
3
5809
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.
12
18626
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
37
11074
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)
5
5802
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...
2
2752
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 ----------------------
0
9645
marktang
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...
0
10325
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
10147
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...
0
9950
tracyyun
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...
0
8972
agi2029
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...
0
6739
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
5381
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...
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
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.