Send a variable to a function and modify this variable in C - c

I have the following vars:
char seed[NBITS + 1], x0[NBITS + 1], y0[NBITS + 1], z0[NBITS + 1], dT0[NBITS + 1];
And i want to change it values on this function:
void lfsr(char *bin, char *output)
{
//bits significativos para fazer o xor NBITS -> NBITS,126,101,99;
int bits[4];
int bit;
if(bin[0] == '0')
bits[0] = 0;
else if(bin[0] == '1')
bits[0] = 1;
if(bin[2] == '0')
bits[1] = 0;
else if(bin[2] == '1')
bits[1] = 1;
if(bin[21] == '0')
bits[2] = 0;
else if(bin[21] == '1')
bits[2] = 1;
if(bin[19] == '0')
bits[3] = 0;
else if(bin[19] == '1')
bits[3] = 1;
bit = bits[0] ^ bits[1] ^ bits[2] ^ bits[3] ^ 1;
//reconstruir o vector de char depois do lfsr
for(int i = 127; i >= 1; i--)
{
bin[i] = bin[i - 1];
}
bin[0] = (char)(48 + bit);
output = bin;
}
The way that I put the value in y0 from x is, for example, calling the lfsr functions like this:
lfsr(x0, y0);
What am I doing wrong?
I have to do 3 Fibonacci Linear Feedback Shift Register starting from x0.
x0 = 10101010101010
y0 = lfsr(101010101010)
z0 = lfsr(y0)
dT0 = lfsr(z0);
The results are good, but when I do the above code the value of x0 will be the same as dT0 if i use pointers.
Can anyone help me?
Thanks. Cumps!

Consider the following:
The numbers correspond to the taps. The bits are actually 15..0, left to right. The following is my implementation of the Fibonacci Linear Feedback Shift Register:
#include <stdio.h>
uint16_t fibLfsr(const uint16_t num)
{
uint16_t tempNum;
tempNum = (num) ^ (num >> 2) ^ (num >> 3) ^ (num >> 5);
tempNum = (tempNum & 0x1) << 15;
tempNum = (tempNum | (num >> 1));
return tempNum;
}
int main(void)
{
uint16_t testNum = 0xACE1;
printf("%#X\n", testNum);
testNum = fibLfsr(testNum);
printf("%#X\n", testNum);
return 0;
}
I'm not quite sure why you're using strings and converting them to binary. If this is necessary, you'll need some of the standard library APIs in stdlib and string to convert the string to an uint16_t before calling fibLfsr() and back to a string afterwards.

Related

Why this error happens while showing array out in C?

What I'm making is input a number that type is 'unsigned long long' and then make it to binary form and show it by 16 figures.
Here's my code and result.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
char D_PAN_ID[16];
char D_ADDRESS[16];
char S_PAN_ID[16];
char S_ADDRESS[16];
int main()
{
int bit1, bit2, bit3, bit4, k1, k2, k3, k4;
unsigned long long dec = 5;
/*
printf("8바이트 정수를 이진수로 변환\n");
scanf("%llu", &dec);
*/
printf("%llu 를(을) 이진수로 변환하면:\n", dec);
for (bit1 = 63; bit1 >= 48; bit1--)
{
k1 = dec >> bit1;
if (k1 & 1) {
D_PAN_ID[63 - bit1] = '1';
printf(&D_PAN_ID[63 - bit1]);
}
else {
D_PAN_ID[63 - bit1] = '0';
printf(&D_PAN_ID[63 - bit1]);
}
}
printf("\n\n");
for (bit2 = 47; bit2 >= 32; bit2--)
{
k2 = dec >> bit2;
if (k2 & 1) {
D_ADDRESS[47 - bit2] = '1';
printf(&D_ADDRESS[47 - bit2]);
}
else {
D_ADDRESS[47 - bit2] = '0';
printf(&D_ADDRESS[47 - bit2]);
}
}
printf("\n\n");
for (bit3 = 31; bit3 >= 16; bit3--)
{
k3 = dec >> bit3;
if (k3 & 1) {
S_PAN_ID[31 - bit3] = '1';
printf(&S_PAN_ID[31 - bit3]);
}
else {
S_PAN_ID[31 - bit3] = '0';
printf(&S_PAN_ID[31 - bit3]);
}
}
printf("\n\n");
for (bit4 = 15; bit4 >= 0; bit4--)
{
k4 = dec >> bit4;
if (k4 & 1) {
S_ADDRESS[15 - bit4] = '1';
printf(&S_ADDRESS[15 - bit4]);
}
else {
S_ADDRESS[15 - bit4] = '0';
printf(&S_ADDRESS[15 - bit4]);
}
}
return 0;
}
And what I got is this. I think it is adding upper result at back since printing second result. Like this:
result1
result2 + result1
result3 + result1 + result2
result4 + result1 + result2 + result3
How do I fix it to show like
000000000000
000000000000
000000000000
000000000101

How do I change the bytes of a 32-bit integer?

I'm trying to add two binary numbers together using only logical statements and binary arithmetic operators. But I'm confused on how to actually change the bits. It is mostly the out variable that I am trying to change, but it keeps getting zeroed every time I print it.
#include <stdio.h>
void execute_add(int a, int b){
int i = 0;
int bit;
int bit2;
int carryOut = 0;
int out = 10;
int overflow = 0;
for(i = 0; i <32 ; i++){
bit = (a >> i) & 1;
bit2 = (b >> i) & 1;
if(bit==1 && bit2==1 && carryOut == 0){
carryOut = 1;
out = 0 | (0x1 >> i);
}else if(bit==1 && bit2==1 && carryOut == 1){
carryOut = 1;
out = 1 | (0x1 >> i);
}else if(bit==0 && bit2==0 && carryOut == 0){
carryOut = 0;
out= 0 | (0x1 >> i);
}else if(bit==0 && bit2==0 && carryOut == 1){
carryOut = 0;
out = 1 | (0x1 >> i);
}else if(bit==1 && bit2==0 && carryOut == 0){
carryOut = 0;
out = 1 | (0x1 >> i);
}else if(bit==1 && bit2==0 && carryOut == 1){
carryOut = 1;
out = 0 | (0x1 >> i);
}else if(bit==0 && bit2==1 && carryOut == 0){
carryOut = 0;
out = 1 | (0x1 >> i);
}else if(bit==0 && bit2==1 && carryOut == 1){
carryOut = 1;
out = 0 | (0x1 >> i);
}else{
}//if else
}//for loop
printf("\n");
bit = (a >> 31) & 1;
bit2 = (a >> 31)& 1;
int bit3 = (out >> 31) & 1;
if( bit == 1 && bit2== 1 && bit3 == 0){
overflow = 1;
}else if (bit == 0 && bit2 == 0 && bit3 == 1){
overflow = 1;
}else{
}//overflow check
int j;
int g = 0;
for(j = 31; j>=0; j--){
if(g%4==0 && g!=0){
printf(" ");
}
bit2 = (out >> j) & 1;
printf("%d", bit2);
g++;
}
printf("\n");
}
int main (){
int a = 34;
int b = 17;
execute_add(a, b);
return 0;
}
With each of these statements in your for loop:
out = 0 | x;
You're resetting out, and clearing away all the work you've already done. You probably mean to do:
out = out | x
Or, equivalently,
out |= x
You also are right-shifting 1 all over the place, which is not what you're looking for; for any shift greater than zero, that's going to give you zero. I think you're often looking for a left-shift where you use a right shift.
I would second kaylum's comment about using a debugger; even if you flip the appropriate shifts and | with out properly, you're still going to have logic errors that will be easily fixed with a debugger.

How is this number expressed as a multiple of 2^-22*(1/10)? How is that derived?

I was refreshing on binary and floats and I run into the following example:
0.1 is represented as: 0.0001100110011001100110011[0011] with the part in the brackets repeating.
If we round up this representation we get:
x = 0.00011001100110011001101 (0.10000002384185791015625 in decimal).
The difference x-0.1 has the binary representation:
0.0000000000000000000000000[1100] which is (0.00000002384185791015625 in decimal).
Now how can this value be expressed as a fraction of 2^x* 1/10?
I have read that it is basically 2^-22*(1/10) but I can't see how we can derive this. Any help?
Note: The numbers are rounded to 23 bits (but the 1 in the x-0.1 example is in the 25th)
Update:
My question is not how 1/10 is represented.
But how from the bit string 0.0000000000000000000000000[1100] we can express it in a "human" format i.e. in decimal. In this case that it was 2^-22*(1/10)
Use the property that an n digit binary number , repeated, has the value of the
pattern *= (1 << BitWidth)/ ((1 << BitWidth) - 1);
The proceed to simplify the fraction.
unsigned gcd(unsigned a, unsigned b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
// Print result & return NULL on success, else point to problem in input.
const char *Cratylus_s(const char *src) {
// parse the input
const char *s = src;
while (*s == '0')
s++;
if (*s != '.') {
return s;
}
s++;
const char *rp = s; // radix point
while (*s == '0')
s++;
int offset = s - rp;
if (*s != '[') {
return s;
}
s++;
unsigned bin = 0;
unsigned pow2 = 0;
while (*s == '0' || *s == '1') {
bin = bin * 2 + *s - '0';
pow2++;
s++;
}
if (*s != ']' || *++s != '\0') {
return s;
}
// multiply `bin` by (1 << pow2)/((1 << pow2 - 1)
unsigned num = bin * (1 << pow2);
unsigned den = (1 << pow2) - 1;
const char *format = "2^%d*(%u/%u)\n";
printf(format, offset + pow2, num, den); // 2^29*(192/15)
// simplify
unsigned common = gcd(num, den);
num /= common;
den /= common;
printf(format, offset + pow2, num, den); // 2^29*(64/5)
// find powers of 10
for (unsigned d = den; d && d % 5 == 0; d /= 5) {
num *= 2;
den *= 2;
}
// find powers of 2
for (unsigned n = num; n && n % 2 == 0; n /= 2) {
num /= 2;
offset--;
}
printf(format, offset + pow2, num, den); // 2^-22*(1/10)
return NULL;
}
void Cratylus_test(const char *s) {
printf("'%s'\n", s);
const char *t = Cratylus_s(s);
printf("'%s'\n", t ? t : "OK");
}
int main(void) {
Cratylus_test("0.0000000000000000000000000[1100]");
Cratylus_test("0.000000000000000[0110]");
return 0;
}
'0.0000000000000000000000000[1100]'
2^29*(192/15)
2^29*(64/5)
2^22*(1/10)
'OK'
'0.000000000000000[0110]'
2^19*(96/15)
2^19*(32/5)
2^13*(1/10)
'OK'

Wrong char value in C

Need some help in the last part of this function:
char *lfsr(char *bin)
{
//bits significativos para fazer o xor 128 -> 128,126,101,99;
int bits[4];
int bit;
if(bin[0] == '0')
bits[0] = 0;
else if(bin[0] == '1')
bits[0] = 1;
if(bin[2] == '0')
bits[1] = 0;
else if(bin[2] == '1')
bits[1] = 1;
if(bin[21] == '0')
bits[2] = 0;
else if(bin[21] == '1')
bits[2] = 1;
if(bin[19] == '0')
bits[3] = 0;
else if(bin[19] == '1')
bits[3] = 1;
bit = bits[0] ^ bits[1] ^ bits[2] ^ bits[3] ^ 1;
//reconstruir o vector de char depois do lfsr
for(int i = 127; i >= 1; i--)
{
bin[i] = bin[i - 1];
}
bin[0] = (int)bit;
return bin;
}
Why the value of bin[0] is a strange character instead 0 or 1?
Anyone can help me please?
Thanks in advance. Cumps!
The problem is character '0' and integer 0 being different. character '0' is actually the ascii value 48.
try bin[0] = (char)(48 + bit);
I'm a C language psychic and my crystal ball tells me that somewhere in your program you did this:
printf("the value in bit[0] is %c\n", bit[0]);
Well, probably one of your if-elses didn't work.
Either bin[0], bin[2], bin[21] or bin[19] is neither '0', nor '1'.
Try running this code with your test data:
char *lfsr(char *bin)
{
//bits significativos para fazer o xor 128 -> 128,126,101,99;
int bits[4];
int bit;
if(bin[0] == '0')
bits[0] = 0;
else
{
if(bin[0] == '1')
bits[0] = 1;
else
printf("First if-else failed. bin[0] : %c\n", bin[0]);
}
if(bin[2] == '0')
bits[1] = 0;
else
{
if(bin[2] == '1')
bits[1] = 1;
else
printf("Second if-else failed. bin[2] : %c\n", bin[2]);
}
if(bin[21] == '0')
bits[2] = 0;
else
{
if(bin[21] == '1')
bits[2] = 1;
else
printf("Third if-else failed. bin[21] : %c\n", bin[21]);
}
if(bin[19] == '0')
bits[3] = 0;
else
{
if(bin[19] == '1')
bits[3] = 1;
else
printf("Fourth if-else failed. bin[19] : %c\n", bin[19]);
}
bit = bits[0] ^ bits[1] ^ bits[2] ^ bits[3] ^ 1;
//reconstruir o vector de char depois do lfsr
for(int i = 127; i >= 1; i--)
{
bin[i] = bin[i - 1];
}
bin[0] = (int)bit;
return bin;
}

C Concatenate int

I will be the first to admit, I'm a C# guy 100% and C isn't for me. However I have problem.
I need to concatenate 7 with HashUrl(HashInt) and then with HashInt
Any help would be greatly appreciated.
int main(int argc)
{
unsigned int HashInt;
HashInt = HashURL(argc);
// I need to return 7 + CheckHash(HashInt) + HashInt but not ADDING, but concanenating them
return HOWEVERTOGETTHESTRING;
}
I should have specified the usage of this. It's actually going to be used in a students VB6 project.
Private Declare Function main Lib "checksum.dll" (ByVal pStr As String) As Long
Private Sub Command1_Click()
MsgBox main("http://hello.com")
End Sub
The full source for the C library is
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <string.h>
#include <winreg.h>
#include <stdlib.h>
int StrToInt(char *pStr, int Init, int Factor)
{
while (*pStr) {
Init *= Factor;
Init += *pStr++;
}
return Init;
}
int HashURL(char *pStr)
{
unsigned int C1, C2, T1, T2;
C1 = StrToInt(pStr, 0x1505, 0x21);
C2 = StrToInt(pStr, 0, 0x1003F);
C1 >>= 2;
C1 = ((C1 >> 4) & 0x3FFFFC0) | (C1 & 0x3F);
C1 = ((C1 >> 4) & 0x3FFC00) | (C1 & 0x3FF);
C1 = ((C1 >> 4) & 0x3C000) | (C1 & 0x3FFF);
T1 = (C1 & 0x3C0) << 4;
T1 |= C1 & 0x3C;
T1 = (T1 << 2) | (C2 & 0xF0F);
T2 = (C1 & 0xFFFFC000) << 4;
T2 |= C1 & 0x3C00;
T2 = (T2 << 0xA) | (C2 & 0xF0F0000);
return (T1 | T2);
}
char CheckHash(unsigned int HashInt)
{
int Check = 0, Flag = 0;
int Remainder;
do {
Remainder = HashInt % 10;
HashInt /= 10;
if (1 == (Flag % 2) ){
Remainder += Remainder;
Remainder = (Remainder / 10) + (Remainder % 10);
}
Check += Remainder;
Flag ++;
} while( 0 != HashInt);
Check %= 10;
if (0 != Check) {
Check = 10 - Check;
if (1 == (Flag % 2)) {
if (1 == (Check % 2)) {
Check += 9;
}
Check >>= 1;
}
}
Check += 0x30;
return Check;
}
int main(int argc)
{
unsigned int HashInt;
int result;
HashInt = HashURL(argc);
char temp[100];
sprintf(temp, "7%i%j", CheckHash(HashInt), HashInt);
result = atoi(temp);
return result;
}
give " http://www.hello.com" should return 783544359868 but its not
You can use sprintf function to create formatted strings. To concatenate 3 integers into a string you could use something like
sprintf(string, "%d%d%d", int1, int2, int3)
I'm not all that great at C, but how I would solve this problem is to first turn them into character arrays, then concatenate those. Then you could turn the result back into an int:
int main(int argc) {
unsigned int HashInt;
int result;
HashInt = HashURL(argc);
char temp[50];
sprintf(temp, "7%i%i", CheckHash(HashInt), HashInt);
result = atoi(temp);
return result;
}
Note: No guarantees on this working...
For starters, you can look at:
asprintf
Just do man asprintf on a linux system.
Here's a link that has a simple example that you can adapt:
http://www.gnu.org/s/libc/manual/html_node/Dynamic-Output.html

Resources