Pages: [1]   Go Down
 Author Topic: Bit manipulation and addition?  (Read 454 times) 0 Members and 1 Guest are viewing this topic.
Offline
Newbie
Karma: 0
Posts: 36
 « on: February 19, 2013, 07:57:00 pm » Bigger Smaller Reset

Hey guys,
I have two 4 bit numbers but I want to "combine" (lack of a better word) these into one 8 bit

For example:
firstnumber = 1010
secondnumber = 1100

I want to put these together literally like:
10101100

How do I do this?

And also, if I and that 8 bit number with 15, will I get the firstnumber? And therefore anding with 240, get the second number?

Cheers, Harris
 Logged

Poole, Dorset, UK
Offline
Edison Member
Karma: 50
Posts: 2207
 « Reply #1 on: February 19, 2013, 08:00:52 pm » Bigger Smaller Reset

first number*8+second number

Mark
 Logged

Offline
Newbie
Karma: 0
Posts: 3
 « Reply #2 on: February 19, 2013, 08:13:36 pm » Bigger Smaller Reset

Actually, it would be firstnumber * 16 + secondnumber.

Although this may be more reliable:

result = (firstnumber & 0x0f) << 4 | (secondnumber & 0x0f)

Therefore:

secondnumber = result & 0x0f

firstnumber = (result & 0xf0) >> 4
 Logged

Offline
Newbie
Karma: 0
Posts: 36
 « Reply #3 on: February 19, 2013, 08:15:01 pm » Bigger Smaller Reset

I tried Marks method and was wondering why it didnt seem to work, I then tried your method zeb and it worked great! Thanks a bunch as per guys
 Logged

Left Coast, CA (USA)
Offline
Brattain Member
Karma: 361
Posts: 17263
Measurement changes behavior
 « Reply #4 on: February 19, 2013, 08:17:10 pm » Bigger Smaller Reset

Actually, it would be firstnumber * 16 + secondnumber.

Although this may be more reliable:

result = (firstnumber & 0x0f) << 4 | (secondnumber & 0x0f)

Therefore:

secondnumber = result & 0x0f

firstnumber = (result & 0xf0) >> 4

Now that is a complete answer. C can be so nice and concise if you let it be.

Mark meant well, just having a bad hair day.

Lefty
 Logged

Offline
Newbie
Karma: 0
Posts: 36
 « Reply #5 on: February 19, 2013, 08:28:03 pm » Bigger Smaller Reset

Actually, it would be firstnumber * 16 + secondnumber.

Although this may be more reliable:

result = (firstnumber & 0x0f) << 4 | (secondnumber & 0x0f)

Therefore:

secondnumber = result & 0x0f

firstnumber = (result & 0xf0) >> 4

Now that is a complete answer. C can be so nice and concise if you let it be.

Mark meant well, just having a bad hair day.

Lefty

No need to worry! I definitely appreciate the help from both
 Logged

Offline
Newbie
Karma: 0
Posts: 3
 « Reply #6 on: February 19, 2013, 08:48:12 pm » Bigger Smaller Reset

Glad to have been of assistance.
 Logged

Poole, Dorset, UK
Offline
Edison Member
Karma: 50
Posts: 2207
 « Reply #7 on: February 20, 2013, 08:02:15 am » Bigger Smaller Reset

Opps, It was the red wine

Mark
 Logged

 Pages: [1]   Go Up