Ok... I did 3-4 project with your help. I have got now three 100 grades and I am so happy thank you all :)
But now I have a huge problem... My new project is mess.......

"""You are been asked to prepare the creation of a pseudo-random number generator. You will simulate the operation of a 8-bit Linear Feedback Shift Register (LFSR)."""

Ok I start to research on LFSR but I only see a register with Right shift 1 bit changing but which order?? I couldnt figure it....

Second question is how can I write hexadecimal numbers? can c recognize hexe??

I will attach my project and please somebody explain me how should I think about it... Then I am gonna write my first try....

Recommended Answers

All 23 Replies

I research on the site and I found hexa code form ancient dragon

void hexaDecimal(int h, char * buffer, int bufferSize){ 
int rem; 
char output[8]= {0}; 
char digit = 0; 
int i = 0; 
int j = 0; 
int k = 0; 
do { 
rem = h%16; 
digit = DecimalToHexa(rem); 
h=h/16; 
output[i] = digit; 
++i; } 
while(h/16!=0); 
if(h > 0) 
{ rem = h; 
digit=DecimalToHexa(rem); 
output[i] = digit; 
++i; } 
if(i >= bufferSize) 
{ buffer[0] = 0; 
} else { 
for(j= i-1, k = 0; j >= 0; j--,k++) 
{ buffer[k] = output[j]; } 
buffer[i] = 0; }} 
char DecimalToHexa(int rem){ 
char c = 0; if( rem >= 10) { 
c = (rem - 10) + 'A'; } 
else c = rem + '0'; 
return c;}

Can it be usefull for me or are there any special code for dcimal convert to hexa??

I research on the site and I found hexa code form ancient dragon

Please don't blame that code on me!:) I didn't write it.

>>Second question is how can I write hexadecimal numbers? can c recognize hexe??

I assume you mean a string that contains hex, such as "0x0a".

opps sorry :)

>Please don't blame that code on me! :) I didn't write it.
Heheh, gotta love being attributed for bad code. ;)

>Second question is how can I write hexadecimal numbers? can c recognize hexe??
C can read and write hexadecimal. Since it's a formatted value, you need to use the %x or %X format modifier for scanf/printf.

>Ok I start to research on LFSR but I only see a register with Right shift 1 bit changing but which order??
Rephrase your question, please.

more simple is how can LFSR work :)

Heelllllllllllllllllllllpppppppppppp :((
I am trying to get my new bit but I coludnt...
My first hex number which is gonna be right or left shift and get my tap positions according to my second HEX numbers 1.
I can convert them hex to binary but I can not take tap position and calculate my new bit :(
of course another poblem is shifting but I think this is more serious shifting might be easy but I have tu find this first please.... :((

#include<stdio.h>
#include<stdlib.h>
void shift(unsigned,unsigned);
int main(){
    int a,b;
    printf("Enter your first HEX number\n");
    scanf("%X",&a);
    printf("Enter your second HEX number for taps\n");
    scanf("%X",&b);
shift(a,b);
system("PAUSE");
return 0;
}
void shift(unsigned value,unsigned tap)
{unsigned c, displayMask = 1 << 7;
printf("Initial position %7u = ", value);
for(c = 1; c <= 8; c++){
value & displayMask ? putchar('1') : putchar('0');
value = value << 1;
}putchar('\n');
unsigned d,newbit,displayMask2 = 1<<7;
printf("Tap positions.. %7u = ",tap);
for(d = 1; d <= 8; d++){
tap & displayMask2 ? putchar('1') : putchar('0');
if(tap==1){
unsigned tappos= value << d;
newbit^=tappos;
printf("new bit %7u = ",newbit);
tap = tap << 1;
}}putchar('\n');
}

... I can not take tap position and calculate my new bit. of course another poblem is shifting but ...

ok, jerry. here is an implementation in C++; i have written it in C++ because a. i find it easier to explain bit-wise operations in code rather than in words. b. writing it in C would be me doing your homework. my suggestion is: ignore the C++ syntax, focus on the logic instead. and if you can treat this as psuedocode and rewrite it in C, you would have solved the problem yourself.

#include<iostream>
#include <bitset>
#include <string>
#include <set>
#include <iomanip>
using namespace std ;

enum { NBITS = 8 };
typedef bitset<NBITS>  shift_register ;
enum bit { zero=0, one=1 };

// returns the bit shifted out
bit shift( shift_register& value, const shift_register& tap_sequence ) ;

int main()
{
  cout << "shift register value (" << NBITS << " bits[0/1]): " ; 
  string value_string ; cin >> value_string ;
  shift_register value( value_string ) ;
  cout <<   value[0] << '\n' ;

  string tap_string  = "10110010" ; // a good tap sequence for 8 bits 
  //cout << "tap_sequence: (" << NBITS << " bits[0/1]): " ;
  //cin >> tap_string ;
  shift_register tap_sequence( tap_string ) ;

  cout << "value: " << value << "\ttap_sequence: " << tap_sequence << '\n' ;

  set<unsigned long> number_stream ; // numbers generated so far
  int period = 0 ;
  while(true)
  {
     string number_string ;
     static const char* const literal_bit = "01" ;
     
     // get the next  NBITS shifted out bits and construct a number out of it
     for( int i=0 ; i<NBITS ; ++i ) 
        number_string += literal_bit[ shift( value, tap_sequence ) ] ;
     unsigned long number = shift_register(number_string).to_ulong() ;

     // if the number is already in the set, we have completed
     // the period of this lfsr.
     // (period is the length of the stream before it repeats)
     if( !number_stream.insert(number).second ) break ;
     
     cout << setw(3) << right << ++period << ". " << number << '\n' ;
  }
}

bit shift( shift_register& value, const shift_register& tap_sequence )
{
  shift_register tapped_bits = value & tap_sequence ;
  // new bit shifted in is 1 if number of tapped bits set is odd; 0 otherwise
  bool new_bit = tapped_bits.count()%2 == 1 ;  

  // lsb in value is the one to be shifted out
  bit shifted_out = value[0] == 0 ? zero : one ;
  value >>= 1U ; value[NBITS-1] = new_bit ;

  return shifted_out ;
}
commented: A good concept of help without actually doing the homework. +19

:( it is really different than c :) Thanx anyway it will be hard but I am trying to understand thnak you....

my last work is here. only problem is I write in the file binary but I have to write hexe.. I think I have to change shift function but I couldnt please help....

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define SIZE 255
#define bit 8
//Functionsint 
getnum(char);
char getchar(int);
int shift(int);
void displayBits(unsigned);
char digit[SIZE];
char digit2[SIZE];
///////////////i
int main (char *argv){
FILE *ptr;
int hex;
char buf[25];
int r;
long i,j;
if ((ptr = fopen( "results.txt", "w" ) ) == NULL ){
printf( "File could not be opened\n" );}
else{printf("Enter a hexadecimal number for initial: ");
scanf ("%X",&i);
ltoa(i,digit,2);
displayBits(i);
printf("Enter a hex number for tap position...: ");
scanf ("%X",&j);ltoa(j,digit2,2);displayBits(j);
r=strlen(digit);
printf("\nResults are in the file\n");
for(int k=0; k<256; k++){
//for(i=0;i<bit-1;i++){
//hex= shift(r)* pow( 2,i);
fprintf(ptr,"%d",shift(r));
if (k%8==7){
fprintf(ptr,"\n");}}
fclose(ptr);}
system("PAUSE");
return 1;}
///////////////////////////////////////int n = 1213;//char buf[25];
//sprintf(buf,"%X", n);
/////////////////////////////////////
void displayBits(unsigned value){
unsigned c, displayMask = 1 << 7;
printf("%u = ", value);
for(c = 1; c <= bit; c++){
value & displayMask ? putchar('1') : putchar('0');value = value << 1;}
putchar('\n');}
//////////////////////////////////////
int getnum(char z){
if (z=='0'){
return 0;}
return 1;}
//////////////////////////////////////
char getchar(int m){
if (m==0){
return '0';}
return '1';}
//////////////////////////////////////
int shift( int x) {
int a;int dot = 0; 
int res = getnum(digit[0]);
for (int a=0; a<x; a++) {
dot += getnum(digit[a]) * getnum(digit2[a]);}
dot = dot%2;
for (a=1; a<x; a++) {
digit[a-1]=digit[a];}
digit[x-1]=getchar(dot);
return res;}
//////////////////////////////////////
commented: Learn to indent your code properly. Not only will it help us but it will help you as well. -2

oops you already correct it... please help :)

change the format string in line 34 to "%x" and it will print hex instead of decimal digits.

fprintf[B]([/B]ptr,"%X ",shift[B]([/B]r[B]))[/B];

>>I think I have to change shift function but I couldnt
what's wrong with it?

I wirte one by one all binary digit with it how can I just change x and convert it I just dont think its work :)

and I tried it didint work :(

line 31: why is it looping 256 times? There are normally only 8 bits in a byte. should use CHAR_BIT macro from limits.h

I think you need to make the conversion in memory then write it out to the file after all shifting is complete.

becouse the homework ask that way :(
it asks check 256 times and write every 8th.
I couldnt understand memory???

you can use strtoul() to convert a string of binary digits to hex or decimal.

I am checking all bits beacause I have to XOR acording to tap positions.. so I did one by one all bit .... Program ask run it 256 times and print every 8.th

you can use strtoul() to convert a string of binary digits to hex or decimal.

then I should my 8 bits binary send to string???? to convert with strtoul to hex???

strtoul() will convert a string of binary digits to decimal, something line atoi(). Then if you want to print hex in a file use the "%x" format

int n = 0;
char ptr = 0;
char bin[] = "01010101";
 
n = strtoul(bin, &ptr, 2);
printf("%x", n);

thanx I am trying

i have to do a project about an lsfr with more than 20 bits.what should i change on the code above?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.