Binary Arithmetics

K

kerbbb

Hello I am trying to add a long to long and I need some help
example:
01101011
00001000
--------
????????
does anyone have any idea how to make a simple function(a,b)

I figure out that if I AND a to be is working but is 1 is ANDing to 1
the next left bit needs to be set as 1.
thanks
 
K

kerbbb

that is cool but I know that part I don't know how I can make write
that in C
thanks anyway
 
W

Walter Roberson

M

Martin Ambuhl

Hello I am trying to add a long to long and I need some help
example:
01101011
00001000

inline long longadd(long a, long b) { return a+b; }
I figure out that if I AND a to be is working but is 1 is ANDing to 1
the next left bit needs to be set as 1.

Who cares?
 
M

Martijn

Hello I am trying to add a long to long and I need some help
example:
01101011
00001000
--------
????????
does anyone have any idea how to make a simple function(a,b)

I figure out that if I AND a to be is working but is 1 is ANDing to 1
the next left bit needs to be set as 1.
thanks

I am not really sure what your intented goal is, but realize this:

result = first XOR second
carry = first AND second

in code that would be:

r = b1 ^ b2;
c = b1 & b2;

Of course you have to incorporate the carry in the following steps as well
(so really you are adding three numbers). But this is trivial stuff for any
CPU, so again am not sure why you'd want C code to accomplish this.

Good luck,
 
K

kerbbb

thanks I figure it out, wasn't a homework sorry ;-)
I was trying to add numbers to time_t types and to change the time
without converting to tm structure.
 
M

marbac

Hello I am trying to add a long to long and I need some help
example:
01101011
00001000
--------
????????
does anyone have any idea how to make a simple function(a,b)

I figure out that if I AND a to be is working but is 1 is ANDing to 1
the next left bit needs to be set as 1.
thanks


Hi,

i would define 2 functions, one fo a half adder, and one for a full adder.

With this functions you can make a n-Bit adder (using the full adder
function recursively)




Definitions:
&& ... logical AND (^)
&& ... logical OR (v)
! ... logical NOT (_)


truth table of Half adder:

A B C Z
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 0

Equations:
Z=(A! && B) || (A && B!)
C=(A && B)

truth table of Full adder:

C B A Cr Z
0 0 0 0 0
0 0 1 0 1
0 1 0 0 1
0 1 1 1 0
1 0 0 0 1
1 0 1 1 0
1 1 0 1 0
1 1 1 1 1

Equations:
Z=(A && B! && C!) || (A! && B && C!) || (A! && B! && C) || (A && B && C)
Cr= (A && B && C!) || (A && B! && C) || (A! && B && C) || (A && B && C)

Karnaughoptimization of Carry:
Cr=(A && B) || (B && C) || (A && C)


Merging HA and FA to (as example) 2-Bit Adder:

Input: 2 2-bit numbers
A1,A0
B1,B0

A0, B0 -> HA -> Z0,C0
A1, B1, Cr1=C0 -> Z1, Carry

Result: Z1,Z0, Carry

If you want to add a 3 Bit number, you just have to extend it using
another Full adder for A2,B2,C2=Carry ... and so on
 
K

Kevin D. Quitt

That's going to be completely non-portable, because there's no guarantee what bits in a time_t
mean.
 
R

Richard Bos

Kevin D. Quitt said:
That's going to be completely non-portable,

_What_ is? You haven't even the excuse of using Google Broken Beta, so
_quote_ something, for goodness' sake!
(Hint: Options -> Posting Prefs -> General -> Include original text.)

Richard
 
K

Keith Thompson

thanks I figure it out, wasn't a homework sorry ;-)
I was trying to add numbers to time_t types and to change the time
without converting to tm structure.

Huh?

The standard only guarantees that time_t is an arithmetic type capable
of representing times.

If you're willing to assume that it represents times monotonically in
seconds, then adding, say, 60 seconds is trivial: t + 60. This
assumption will be valid for many systems, but of course it's strictly
non-portable.

If you're not willing to make this assumption, you pretty much need to
use "struct tm" if you want to manipulate times.

In neither case, as far as I can tell, does it make any sense to
emulate ordinary arithmetic by manually twiddling bits. If you think
it does, then either you're mistaken or I'm missing something about
the problem you're trying to solve.

You'll often get more useful help if you'll tell us *why* you're
trying to do something.

<METAPHOR>
If you ask us for a screwdriver with a really strong handle, we can
probably tell you how to get one. If you ask us for a screwdriver
with a really strong handle so you can drive nails with it, we can
tell you to put down the screwdriver and get a hammer.
</METAPHOR>
 
K

Kevin D. Quitt

_quote_ something, for goodness' sake!

I'm so sick and tired of people quoting entire articles for a "me, too", that I guess I went a
little far in the other direction. FWIW: he wanted to do arithmetic on a time_t.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,901
Members
47,439
Latest member
elif2sghost

Latest Threads

Top