473,387 Members | 1,569 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,387 software developers and data experts.

Simple Bitmask problem

Hi there,

I have a simple problem to solve but somehow I am too stupid to figure
out where is the error.
Basically, I wanna shift each element of the array by an amount of n,
whereas the n highest
bits shifted out should be the n-least significant bits of the next
higher word. I tried doing this
by shifting the words the amount of n to the left which works fine. In
the next step I shift the lower
word 32-n positions to the right and OR the result with the higher
word which should add the then
bits I shifted out in the former step. Here is the code:

uint32 p_lo[] = { 0x00, 0x00, 0x00, 0x00 };

for (j = 0; j < 64; j ++ )
{
p_lo[0] <<= (j % 32);
p_lo[1] <<= (j % 32);
p_lo[1] |= (p_lo[0] >(32 - (j % 32)));
}

Unfortuantely the outcome is nonsense... Anyone an idea what I am
missing?

Many thanks,
Patrick

Jul 18 '07 #1
3 1971
Patrick wrote:
>
Hi there,

I have a simple problem to solve but somehow I am too stupid to figure
out where is the error.
Basically, I wanna shift each element of the array by an amount of n,
whereas the n highest
bits shifted out should be the n-least significant bits of the next
higher word. I tried doing this
by shifting the words the amount of n to the left which works fine. In
the next step I shift the lower
word 32-n positions to the right and OR the result with the higher
word which should add the then
bits I shifted out in the former step. Here is the code:

uint32 p_lo[] = { 0x00, 0x00, 0x00, 0x00 };

for (j = 0; j < 64; j ++ )
{
p_lo[0] <<= (j % 32);
p_lo[1] <<= (j % 32);
p_lo[1] |= (p_lo[0] >(32 - (j % 32)));
}

Unfortuantely the outcome is nonsense... Anyone an idea what I am
missing?
/* BEGIN new.c output */

D_Word[0] is 0x12345678
D_Word[1] is 0x9abcdef0
LEFT_SHIFT 12
D_Word[0] is 0x45678000
D_Word[1] is 0xcdef0123

/* END new.c output */
/* BEGIN new.c */

#include <stdio.h>

#define LEFT_SHIFT 12

int main(void)
{
long unsigned D_Word[] = {0x12345678, 0x9abcdef0};

puts("/* BEGIN new.c output */\n");
printf("D_Word[0] is 0x%x\n", D_Word[0]);
printf("D_Word[1] is 0x%x\n", D_Word[1]);
puts("LEFT_SHIFT 12");
D_Word[1] <<= LEFT_SHIFT;
D_Word[1] |= D_Word[0] >32 - LEFT_SHIFT;
D_Word[0] <<= LEFT_SHIFT;
printf("D_Word[0] is 0x%x\n", D_Word[0]);
printf("D_Word[1] is 0x%x\n", D_Word[1]);
puts("\n/* END new.c output */");
return 0;
}

/* END new.c */

--
pete
Jul 18 '07 #2
pete wrote:
>
long unsigned D_Word[] = {0x12345678, 0x9abcdef0};

is 0x%x\n", D_Word[
is 0x%x\n", D_Word[
is 0x%x\n", D_Word[
is 0x%x\n", D_Word[
Those should all be:

is 0x%lx\n", D_Word[

instead.

--
pete
Jul 18 '07 #3
Patrick wrote:
Basically, I wanna shift each element of the array by an amount of n,
whereas the n highest
bits shifted out should be the n-least significant bits of the next
higher word. I tried doing this
by shifting the words the amount of n to the left which works fine. In
the next step I shift the lower
word 32-n positions to the right and OR the result with the higher
word which should add the then
bits I shifted out in the former step. Here is the code:

uint32 p_lo[] = { 0x00, 0x00, 0x00, 0x00 };

for (j = 0; j < 64; j ++ )
{
p_lo[0] <<= (j % 32);
p_lo[1] <<= (j % 32);
p_lo[1] |= (p_lo[0] >(32 - (j % 32)));
}

Unfortuantely the outcome is nonsense... Anyone an idea what I am
missing?
In my opinion, the most important missing item is a rigorous
specification. Off the top of my head, I expect to know what input to
be shifted, the number of bits to shift, the shift count, what bits are
shifted in, the type of array elements, how many bits are in each array
element, and which array element is considered the most significant.

Given a rigorous specification, you could write the function code and I
could write a test determine if the code works as specified without
looking at the code. Try writing a synopsis for your shift function
that includes a full specification. Writing, testing, and using the
code then becomes a lot easier. ;-)
--
Thad
Jul 19 '07 #4

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

Similar topics

1
by: Andrey Brozhko | last post by:
Hi I need to represent custom type system (ints, bytes, chars, enums, bitmasks, arrays and some other types) in xml. It is easy to see how to represent enums in xml (using xs:enumeration), the...
3
by: Mark Allison | last post by:
Hi, Total newbie here, I'm trying to examine a bitmask which is passed into my app as a string. I convert it to int, and try and process it. I got the following code: //dbStatus current is a...
4
by: Paul Steele | last post by:
I'm working on a programming project that needs to control network traffic. What I would like to do implement a mini-firewall as part of my program. What would be involved with writing a firewall...
7
by: SL | last post by:
Could someone direct to a simple explanation of how to use MS Visual Studio 2003 to design .aspx code? This code works if pasted into notepad and run as an .aspx file: <%@ Import...
18
by: Sender | last post by:
Yesterday there was a very long thread on this query. (You can search on this by post by 'sender' with subject 'Simple Problem' post date Oct 7 time 1:43p) And in the end the following code was...
5
by: Andy | last post by:
Sigh... working on it for whole day and got no idea... Basically I want to have a bitmask filtering function, I will throw in 3 parameters: bitMaskValue - current bitmask value filterValue -...
3
by: TD | last post by:
Is there a way to DataBind controls to a specific Bit in Bitmask? Here is some sample code of what I am trying to do ... I am trying to bind the Visible and Enabled properties of a Button to...
10
by: lithiumcat | last post by:
Hi, This question seems to come up quite often, however I haven't managed to find an answer relevant to my case. I often use binary flags, and I have alaways used a "bitmask" technique, that...
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:
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...
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...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.