473,498 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

look up tables

Hi

I would like to realise a simple lookup table in C. I have
got a const array with 16 values. I have 4 input values of
type integer with 32 bits. I loop then over the single bits
and in each iteration I would like to use the current 4 bits
of the 4 input operands to make a table lookup. I thought of
doing it with bitmasks. Is this a nice way to do it or is there
a more efficient way?

int LUT[16] = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];

int A.B,C,D = rand();
int mask = 1;

int i;
int tmp;
for(i = 0; i < 32; i++) {
tmp = 8*(A & mask) + 4*(B & mask) + 2*(C & mask)+ (D & mask);
lookup_val[i] = LUT[tmp];
mask<<1;
}

Many thanks
Nov 29 '07 #1
5 1935
Mike wrote On 11/29/07 12:34,:
Hi

I would like to realise a simple lookup table in C. I have
got a const array with 16 values. I have 4 input values of
type integer with 32 bits. I loop then over the single bits
and in each iteration I would like to use the current 4 bits
of the 4 input operands to make a table lookup. I thought of
doing it with bitmasks. Is this a nice way to do it or is there
a more efficient way?

int LUT[16] = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];

int A.B,C,D = rand();
int mask = 1;

int i;
int tmp;
for(i = 0; i < 32; i++) {
tmp = 8*(A & mask) + 4*(B & mask) + 2*(C & mask)+ (D & mask);
lookup_val[i] = LUT[tmp];
mask<<1;
}

Many thanks
Nov 29 '07 #2

(Apologies for the earlier incomplete reply.)

Mike wrote On 11/29/07 12:34,:
Hi

I would like to realise a simple lookup table in C. I have
got a const array with 16 values. I have 4 input values of
type integer with 32 bits. I loop then over the single bits
and in each iteration I would like to use the current 4 bits
of the 4 input operands to make a table lookup. I thought of
doing it with bitmasks. Is this a nice way to do it or is there
a more efficient way?

int LUT[16] = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];

int A.B,C,D = rand();
Peculiar syntax ...
int mask = 1;

int i;
int tmp;
for(i = 0; i < 32; i++) {
tmp = 8*(A & mask) + 4*(B & mask) + 2*(C & mask)+ (D & mask);
Note that this calculation will not necessarily yield a
value in the range 0..15. (Hint: What happens on the second
iteration, if A is equal to 2?)
lookup_val[i] = LUT[tmp];
Since LUT[tmp] == tmp for all valid tmp, why bother with
a lookup table at all?
mask<<1;
This will make trouble the last two times through the
loop. At the end of the i==30 iteration you'll try to
double a `mask' value that is bigger than half the maximum
possible `int' value, so the result won't fit in an `int'
(assumed to be 32 bits in this case). Pretty much anything
at all can happen, but the most likely outcome is that
`mask' will become a very large *negative* number -- and
this will raise yet another kind of Hell with the calculation
of `tmp' the next time through the loop.

On the final iteration, you'll try to double that very
large negative `mask', and again the result will be out of
range for an `int', and again anything at all might happen.

This is why bit-fiddling is almost always done with
`unsigned' types, where there are no sign bits to cause
trouble.
}
--
Er*********@sun.com

Nov 29 '07 #3
Joe
On Nov 29, 12:34 pm, Mike <M...@yahoo.co.ukwrote:
Hi

I would like to realise a simple lookup table in C. I have
got a const array with 16 values. I have 4 input values of
type integer with 32 bits. I loop then over the single bits
and in each iteration I would like to use the current 4 bits
of the 4 input operands to make a table lookup. I thought of
doing it with bitmasks. Is this a nice way to do it or is there
a more efficient way?

int LUT[16] = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];

int A.B,C,D = rand();
int mask = 1;

int i;
int tmp;
for(i = 0; i < 32; i++) {
tmp = 8*(A & mask) + 4*(B & mask) + 2*(C & mask)+ (D & mask);
lookup_val[i] = LUT[tmp];
mask<<1;

}

Many thanks
I think you are assuming that "(A & mask)" is a boolean, returning 0
or 1. It is not. Maybe do this instead:
tmp = ((A & mask)?8:0) | ((B & mask)?4:0) | ((C & mask)?2:0) | ((D
& mask)?1:0);

There may be a better way to to this in assembly, which often has
various bit shifting/fiddling functions. In C, you have to rely on the
optimizer to make use of assembly features. So, if speed is critical,
the best approach is just to try it a few different ways and time it.

Joe Krahn
Nov 29 '07 #4
Mike wrote:
>
I would like to realise a simple lookup table in C. I have
got a const array with 16 values. I have 4 input values of
type integer with 32 bits. I loop then over the single bits
and in each iteration I would like to use the current 4 bits
of the 4 input operands to make a table lookup. I thought of
doing it with bitmasks. Is this a nice way to do it or is there
a more efficient way?

int LUT[16] = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];

int A.B,C,D = rand();
int mask = 1;

int i;
int tmp;
for(i = 0; i < 32; i++) {
tmp = 8*(A & mask) + 4*(B & mask) + 2*(C & mask)+ (D & mask);
lookup_val[i] = LUT[tmp];
mask<<1;
}
Hasn't a chance of working. To start with, why are you declaring
the B field of A? Anyhow, A.B and C are initialized to random
values, and D is initialized to a random value with an undeclared
routine (no #includes shown). Then you are using struct A, and
later field B, as integers. Then you are left shifting an integer,
that is initialized to 1, 32 times and discarding the result.
Almost certainly that causes an overflow and undefined behaviour or
it does absolutely nothing.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Nov 29 '07 #5
CBFalconer wrote:
Mike wrote:
>I would like to realise a simple lookup table in C. I have
got a const array with 16 values. I have 4 input values of
type integer with 32 bits. I loop then over the single bits
and in each iteration I would like to use the current 4 bits
of the 4 input operands to make a table lookup. I thought of
doing it with bitmasks. Is this a nice way to do it or is there
a more efficient way?

int LUT[16] = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];

int A.B,C,D = rand();
int mask = 1;

int i;
int tmp;
for(i = 0; i < 32; i++) {
tmp = 8*(A & mask) + 4*(B & mask) + 2*(C & mask)+ (D & mask);
lookup_val[i] = LUT[tmp];
mask<<1;
}

Hasn't a chance of working. To start with, why are you declaring
the B field of A? Anyhow, A.B and C are initialized to random
values,
They are /not/ initialized to any value at all. Not even a "random"
value, which is a confusing term in this context. Whatever value they
happen to start with cannot be used in a strictly conforming program.
and D is initialized to a random value with an undeclared
routine (no #includes shown).
That's fair enough, because this is not a complete program, it's a code
snippet, as evidenced by the for loop outside of a function.
Then you are using struct A, and
later field B, as integers.
In clearer words, "You misspelled A,B,C,D as A.B,C,D".
Then you are left shifting an integer,
that is initialized to 1, 32 times and discarding the result.
Almost certainly that causes an overflow and undefined behaviour or
it does absolutely nothing.
1<<1 cannot possibly cause an overflow, and no other shifting takes
place in the program. (I hadn't noticed that he was discarding the
result, good catch.)
Nov 30 '07 #6

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

Similar topics

6
6748
by: Andreas Lauffer | last post by:
I changed from Access97 to AccessXP and I have immense performance problems. Details: - Access XP MDB with Jet 4.0 ( no ADP-Project ) - Linked Tables to SQL-Server 2000 over ODBC I used...
1
2423
by: MB | last post by:
I need to develop a Cold Fusion application using SQL tables, I am not sure how to setup my tables or that this is the optimal way of setting my tables for the application that I am trying top...
6
2041
by: Doug | last post by:
Hi all. Have a SQL server with about 10 small databases on it. One database in particular, the tables are somehow being recreated each night at 12:03 AM. The tables are also empty of data. There's...
44
3830
by: Mariusz Jedrzejewski | last post by:
Hi, I'll be very grateful if somebody can explain me why my Opera 7.23 (runing under linux) doesn't show me inner tables. Using below code I can see only "inner table 1". There is no problem with...
1
4176
by: Prakash RudraRaju | last post by:
Hi, I have recently migrated MSAccess Tables (nearly 120) to MySQL. To test successful migration I want to compare all tables. I have linked MySQL tables through ODBC connection. I want to...
2
7359
by: Scamjunk | last post by:
I have been desperately looking for a treeview-type solution for my problem for the past three weeks and have been greatly unsuccessful. I am totally new to the world of XSLT and I *don't know*...
4
3170
by: Laurel | last post by:
Is there a way, in the debugger, to look at the contents of a dataset? I tried drilling down into my dataset and it's like exploring the registry! Anyway, I didn't find anything useful. The code...
10
7669
by: Jim Devenish | last post by:
I have a split front end/back end system. However I create a number of local tables to carry out certain operations. There is a tendency for the front end to bloat so I have set 'compact on...
32
2689
by: Simon Dean | last post by:
Duh... Not another one... Hopefully simple though, I hate the way *I* (and it might be a CSS trait) can't intermix fixed width divisional elements with a variable auto expanding div??? The...
12
6965
by: nyathancha | last post by:
Hi, I have a question regarding best practices in database design. In a relational database, is it wise/necessary to sometimes create tables that are not related to other tables through a...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7165
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7205
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6887
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4590
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.