Bit Arithmetic Operation (+, -, /, *)

Thread Starter

em_cardc

Joined Apr 5, 2016
43
Hello.

I am trying to make a program for a microcontroller in C language which covers operation of bits representing decimal numbers.

What I mean with this is:

I have x number of LEDs which will work as outputs and x number of input buttons to perform operations which will then create results both in whole numbers and percentage numbers (fractions such as 5.25, 5.50, 5.75) From the little knowledge I have I know that this can be done using bitwise operations such as shifts and &, oring, and xoring.

I am having trouble creating an algorithm that will input said numbers to be calculated....

x = 15 (1111) / y = 4 (0100) = 3.75 (Which I know that will require per se (3 LEDs that represent the decimal 111 = .75, 010 = .50, 001 = .25) and (4 LEDS or more to represent the whole number).

I do not know if I am right with the kind of algorithm I want to create but I am kind of stuck on how shifts can create the operations of binary numbers representing decimal numbers and to conserve the desired results in binary.
 

MrChips

Joined Oct 2, 2009
34,807
You don't have to do anything.
Choose how many bits you want for whole numbers and how many bits to be fractional numbers.
Put a decimal point between the two.
Each bit to the left of the decimal point represents increasing powers of 2, i.e.
2^0
2^1
2^2
2^3 etc.

Each bit to the right of the decimal point represents negative powers of 2, i.e.
2^-1
2^-2
2^-3
2^-4 etc.
 

Thread Starter

em_cardc

Joined Apr 5, 2016
43
You don't have to do anything.
Choose how many bits you want for whole numbers and how many bits to be fractional numbers.
Put a decimal point between the two.
Each bit to the left of the decimal point represents increasing powers of 2, i.e.
2^0
2^1
2^2
2^3 etc.

Each bit to the right of the decimal point represents negative powers of 2, i.e.
2^-1
2^-2
2^-3
2^-4 etc.
I think I kind of see what this means. Basically I would have 4 Leds (1111) to represent from 1-15 to my right.....

then, a LED dedicated to a decimal point?

and to the right whatever the decimal point it may be? (0.25,0.5,0.75)?
 

MrChips

Joined Oct 2, 2009
34,807
I think I kind of see what this means. Basically I would have 4 Leds (1111) to represent from 1-15 to my right.....

then, a LED dedicated to a decimal point?

and to the right whatever the decimal point it may be? (0.25,0.5,0.75)?
Something like that.
More precisely,
1111.001 = 15.125
1111.010 = 15.25
1111.011 = 15.375
1111.100 = 15.5
1111.101 = 15.625
1111.110 = 15.75
1111.111 = 15.875
 
Top