473,385 Members | 1,317 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Can some one help with this problem of bits?

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
Nov 14 '05 #1
2 1198
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

Nov 14 '05 #2
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

Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Grumfish | last post by:
In order to familiarize my self with Flash files and their bytecode I've started to make an assembler. My first problem is writing the bitfields the format uses often. It is a series of fields,...
6
by: Brian K. Michalk | last post by:
I have a perl app that is calculating the standard deviation of a 4000 element 16 bit integer array, that has large dynamic content. I.e, the range spans a significant portion of the 16 bits. I...
4
by: Andre Paim Lemos | last post by:
Hi, I'm having some compiler problems when I try to use make_heap(), push_heap() and pop_heap(). I am compiling my code on gcc version 3.3.1 (SuSE Linux). I am using the heap related methods to...
2
by: Joel | last post by:
I am having some problems compiling my code on Mandrake 10 with g++ (GCC 3.3.2). The problem seems to be in that I try to define a functor that compares two pointer objects, and use that functor to...
36
by: Profetas | last post by:
Hi, I want to generate a random 8 bit number using rand(0 is that possible? to expecifu the base and the lenght? thanks
11
by: bogusexception | last post by:
(or.. "I'm getting too much Tails and not enough Heads") I'm running into a very strange problem with random numbers and long numbers. To demonstrate the problem, I've created a simple test....
29
by: Halid Umar A M | last post by:
Hi All, I m Halid Umar, network security research student. I will explain my situation and give me hint if you can. I expect reply from you all. * I have to handle numbers that have more than...
2
by: skalig1 | last post by:
Hey the title was just to attract people to my problem.......well my problem is .... i code in vc++ and i have a situation which is like this....... i have a buffer which holds data in the...
39
by: rembremading | last post by:
Hi all! The following piece of code has (for me) completely unexpected behaviour. (I compile it with gcc-Version 4.0.3) Something goes wrong with the integer to float conversion. Maybe somebody...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.