Can some one help with this problem of bits?

S

s.subbarayan

Dear all,
I have one peculiar problem with me for which I need ur inputs how
to implement it:

I have 2 registers each 8 bit wide.

The first register stores:

Register Map:
________________________________ Can be zero or 1.
|
0 0/1 N10 N9 N8 N7 N6 N5-Reg1
N4 N3 N2 N1 0 1 0 0 -Reg2 For other then N10 to N1 values in bits
are constants.

Now the problem is I have calculated value of N and when N is
expressed in binary in 10 bits,I will get N10-N1 bits.How to fill them
in this registers?
What sort of binary arithmetic should I use to fill the details in
this registers. Can some one let me know some logic to do this in a
efficient manner?

What sort of datatype is suitable to store N ?Which type of data will
make it more efficient in storing?

One more problem here is I want to give user the freedom to change
value of 2nd bit(shown as 0/1 in register map above).How to achieve
this in code?

expecting ur reply and advanced thanks,
Regards,
s.subbarayan
 
M

Michael Mair

Hiho,
I have one peculiar problem with me for which I need ur inputs how
to implement it:

I have 2 registers each 8 bit wide.

The first register stores:

Register Map:
________________________________ Can be zero or 1.
|
0 0/1 N10 N9 N8 N7 N6 N5-Reg1
N4 N3 N2 N1 0 1 0 0 -Reg2 For other then N10 to N1 values in bits
are constants.

Now the problem is I have calculated value of N and when N is
expressed in binary in 10 bits,I will get N10-N1 bits.How to fill them
in this registers?
What sort of binary arithmetic should I use to fill the details in
this registers. Can some one let me know some logic to do this in a
efficient manner?

Write it down, go through systematically. Below is a possible way; I did
not test it, though

In your header:


/* RegHi
** Bits:
** 0-5 N5-N10
** 6 Toggle
** 7 0
*/

/* RegLo
** Bits:
** 0-3 0x4
** 4-7 N1-N4
*/

#define N_LO_SHIFT 4
#define N_HI_SHIFT 4

#define N_LO_MASK 0xF
#define N_HI_MASK 0x3F0

#define LO_FIXED (1<<2)
#define HI_FIXED (0<<7)

#define TOGGLE_SHIFT 6
#define TOGGLE_MASK 0x1

#define MAKE_LO(n) 0xFF & (LO_FIXED\
| (((n)&N_LO_MASK)<<N_LO_SHIFT)

#define MAKE_HI(n,toggle) 0xFF & (HI_FIXED\
| (((n)&N_HI_MASK)>>N_HI_SHIFT)\
| (((toggle)&TOGGLE_MASK)<<TOGGLE_SHIFT)

In your program
reghi = MAKE_HI(my_n,1)
reglo = MAKE_LO(my_n)

> What sort of datatype is suitable to store N ?Which type of data will
> make it more efficient in storing?

C99: uint8_t or uint_fast8_t (<stdint.h>)
otherwise: unsigned char

If you want, you can of course use a >=16 bit (unsigned) datatype.

> One more problem here is I want to give user the freedom to change
> value of 2nd bit(shown as 0/1 in register map above).How to achieve
> this in code?

Taken care of. The mask does not make sense for a toggle bit, you might
want to change that part of MAKE_HI to
| ((_Bool)(toggle)<<TOGGLE_SHIFT)

Once more: Only an untested example.


Cheers,
Michael
 
M

Michael Mair

Hiho,

> I have one peculiar problem with me for which I need ur inputs how
> to implement it:
>
> I have 2 registers each 8 bit wide.
>
> The first register stores:
>
> Register Map:
> ________________________________ Can be zero or 1.
> |
> 0 0/1 N10 N9 N8 N7 N6 N5-Reg1
> N4 N3 N2 N1 0 1 0 0 -Reg2 For other then N10 to N1 values in bits
> are constants.
>
> Now the problem is I have calculated value of N and when N is
> expressed in binary in 10 bits,I will get N10-N1 bits.How to fill them
> in this registers?
> What sort of binary arithmetic should I use to fill the details in
> this registers. Can some one let me know some logic to do this in a
> efficient manner?

Write it down, go through systematically. Below is a possible way; I did
not test it, though

In your header:


/* RegHi
** Bits:
** 0-5 N5-N10
** 6 Toggle
** 7 0
*/

/* RegLo
** Bits:
** 0-3 0x4
** 4-7 N1-N4
*/

#define N_LO_SHIFT 4
#define N_HI_SHIFT 4

#define N_LO_MASK 0xF
#define N_HI_MASK 0x3F0

#define LO_FIXED (1<<2)
#define HI_FIXED (0<<7)

#define TOGGLE_SHIFT 6
#define TOGGLE_MASK 0x1

#define MAKE_LO(n) 0xFF & (LO_FIXED\
| (((n)&N_LO_MASK)<<N_LO_SHIFT)

#define MAKE_HI(n,toggle) 0xFF & (HI_FIXED\
| (((n)&N_HI_MASK)>>N_HI_SHIFT)\
| (((toggle)&TOGGLE_MASK)<<TOGGLE_SHIFT)

In your program
reghi = MAKE_HI(my_n,1)
reglo = MAKE_LO(my_n)

> What sort of datatype is suitable to store N ?Which type of data will
> make it more efficient in storing?

I cannot tell you that without knowing what kind of registers these
are. If you need to be free of alignment issues: unsigned char.
Without any restrictions:
C99: uint8_t or uint_fast8_t (<stdint.h>)
otherwise: unsigned char

If you want and this is possible, you can of course use a >=16 bit
(unsigned) datatype for storing both registers. However, without
more information I cannot tell anysthing.

> One more problem here is I want to give user the freedom to change
> value of 2nd bit(shown as 0/1 in register map above).How to achieve
> this in code?

Taken care of. The mask does not make sense for a toggle bit, you might
want to change that part of MAKE_HI to
| ((_Bool)(toggle)<<TOGGLE_SHIFT)

Once more: Only an untested example.


Cheers,
Michael
 

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

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,244
Latest member
cryptotaxsoftware12

Latest Threads

Top