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....
>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.
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 ;
}
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;}
//////////////////////////////////////
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