Problem with endian notation and bit masks. Need suggestion.

B

bobrics

Hi,
I am trying to understand what is wrong with my code. What I have below
is a part of a linear feedback shift register. But it's simplified to
the point where it does not matter. The main problem is bit masks and
endian notation, in my opinion.

If *lfsr is 0x80000000 and the value of temp_bitmask is 0x00000080,
then their bitwise AND should be all zeroes, isn't it? The debugger
prints out that the answer is 0x1 somehow, which 0x00000001. What is
wrong?

The target endianness is set automatically (currently little endian)

Thank you in advance!


#include <stdio.h>
#define INPUT_BIT_MASK 0x80000000

void main (void)
{
int i = 0;
unsigned int lfsr[] = 0x80000000;
unsigned int temp_bitmask;

temp_bitmask = INPUT_BIT_MASK;
temp_bitmask = temp_bitmask >> 28;

printf("the value of temp_bitmask is %d or %x\n", (unsigned int)
temp_bitmask, temp_bitmask);
printf("the value at *lfsr is %d or %x\n", *lfsr, *lfsr);
printf("the value of (temp_bitmask && *lfsr) is %d or %x\n",
(temp_bitmask && *lfsr), (temp_bitmask && *lfsr));

return 0;
}
 
J

Jordan Abel

Hi,
I am trying to understand what is wrong with my code. What I have below
is a part of a linear feedback shift register. But it's simplified to
the point where it does not matter. The main problem is bit masks and
endian notation, in my opinion.

If *lfsr is 0x80000000 and the value of temp_bitmask is 0x00000080,
then their bitwise AND should be all zeroes, isn't it? The debugger
prints out that the answer is 0x1 somehow, which 0x00000001. What is
wrong?

&& is not a bitwise AND.
The target endianness is set automatically (currently little endian)

Thank you in advance!


#include <stdio.h>
#define INPUT_BIT_MASK 0x80000000

void main (void)
{
int i = 0;
unsigned int lfsr[] = 0x80000000;
unsigned int temp_bitmask;

temp_bitmask = INPUT_BIT_MASK;
temp_bitmask = temp_bitmask >> 28;

This gives 0x00000008, not 0x00000080 as you claim.
 
F

Flash Gordon

bobrics said:
Hi,
I am trying to understand what is wrong with my code. What I have below
is a part of a linear feedback shift register. But it's simplified to
the point where it does not matter. The main problem is bit masks and
endian notation, in my opinion.

Yes, you have a definite C language problem or two...
If *lfsr is 0x80000000 and the value of temp_bitmask is 0x00000080,
then their bitwise AND should be all zeroes, isn't it? The debugger
prints out that the answer is 0x1 somehow, which 0x00000001. What is
wrong?

The target endianness is set automatically (currently little endian)

Thank you in advance!


#include <stdio.h>
#define INPUT_BIT_MASK 0x80000000

void main (void)

The only standard return type for main is int. The only good reason to
use anything else is if you are programming a freestanding (embedded)
system.

int main(void)
{
int i = 0;

The great horizontal space shortage finished years ago, so please use
spaces to indent you code to make it readable.
unsigned int lfsr[] = 0x80000000;

I assume in your real code you have more than one value ;-)
unsigned int temp_bitmask;

temp_bitmask = INPUT_BIT_MASK;
temp_bitmask = temp_bitmask >> 28;

printf("the value of temp_bitmask is %d or %x\n", (unsigned int)
temp_bitmask, temp_bitmask);
printf("the value at *lfsr is %d or %x\n", *lfsr, *lfsr);
printf("the value of (temp_bitmask && *lfsr) is %d or %x\n",
(temp_bitmask && *lfsr), (temp_bitmask && *lfsr));

This is the problem you were looking for. && is logical and, i.e. if
both operands are non-0 it returns 1. What you wanted was the bitwise
and operator which is &.
return 0;

Hmm. You said main was void yet return a number, your compiler should
have complained...

Of course, if you define main properly as returning an int you *should*
return a value, and 0 is a portable return value.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top