The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Bitwise help
Discuss Bitwise help in the C Programming forum on Dev Shed. Bitwise help C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 2nd, 2010, 09:51 PM
|
|
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.
|

September 2nd, 2010, 10:14 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
Spoiler: a left shift is the same as multiplying by two. The reverse is also true.
|

September 2nd, 2010, 11:05 PM
|
|
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.
|

September 3rd, 2010, 12:09 AM
|
 |
Lord of Dorkness
|
|
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
|
|
|
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.
|

September 3rd, 2010, 01:12 AM
|
|
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?
|

September 3rd, 2010, 07:03 AM
|
 |
Lord of Dorkness
|
|
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
|
|
|
You want spoon fed, too?
|

September 3rd, 2010, 07:45 AM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
Translation: try it and see.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|