Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

#1
September 2nd, 2010, 09:51 PM
 Archeleus
Registered User

Join Date: Jul 2010
Posts: 13
Time spent in forums: 1 h 23 m
Reputation Power: 0
Bitwise help

How to append "1" to the end of an unsigned integer? (In binary representation)

What do you think is the most efficient way to add two integers using bitwise operations?

Thank you.

#2
September 2nd, 2010, 10:14 PM
 requinix
Still alive

Join Date: Mar 2007
Location: Washington, USA
Posts: 12,869
Time spent in forums: 5 Months 1 Week 5 Days 6 h 19 m 22 sec
Reputation Power: 8977
Spoiler: a left shift is the same as multiplying by two. The reverse is also true.

#3
September 2nd, 2010, 11:05 PM
 Archeleus
Registered User

Join Date: Jul 2010
Posts: 13
Time spent in forums: 1 h 23 m
Reputation Power: 0
yeah I knew that

But that's not the question here.

#4
September 3rd, 2010, 12:09 AM
 DaWei_M
Lord of Dorkness

Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
Posts: 8,515
Time spent in forums: 4 Weeks 18 h 59 m 31 sec
Warnings Level: 20
Number of bans: 3
Reputation Power: 3268
Really, it IS the question. If you're going to append 1 bit then you have to shift the other bits over. Consequently, to append a 1 you multiply by 2 then add 1.

Binary addition is an xor with carry.
__________________
Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
DaWei on Pointers Politically Incorrect.

#5
September 3rd, 2010, 01:12 AM
 Archeleus
Registered User

Join Date: Jul 2010
Posts: 13
Time spent in forums: 1 h 23 m
Reputation Power: 0
Quote:
 Originally Posted by DaWei_M Really, it IS the question. If you're going to append 1 bit then you have to shift the other bits over. Consequently, to append a 1 you multiply by 2 then add 1. Binary addition is an xor with carry.

When looking at it in that way it dies make sense. So the addition you mentioned is just normal x+1 addition?

#6
September 3rd, 2010, 07:03 AM
 DaWei_M
Lord of Dorkness

Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
Posts: 8,515
Time spent in forums: 4 Weeks 18 h 59 m 31 sec
Warnings Level: 20
Number of bans: 3
Reputation Power: 3268
You want spoon fed, too?

#7
September 3rd, 2010, 07:45 AM
 requinix
Still alive

Join Date: Mar 2007
Location: Washington, USA
Posts: 12,869
Time spent in forums: 5 Months 1 Week 5 Days 6 h 19 m 22 sec
Reputation Power: 8977
Translation: try it and see.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Bitwise help