Skip to content

Commit 2c697d1

Browse files
committed
1.1: bit operations, remove math, time gain 50-70%
1 parent 20ba0f9 commit 2c697d1

File tree

3 files changed

+23
-25
lines changed

3 files changed

+23
-25
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
CC = gcc
22
CFLAGS = -Wall -std=c99
3-
LDFLAGS = -lm -s
3+
LDFLAGS = -s
44
PROGS = hammingenc hammingdec
55

66
all: $(PROGS)

hammingdec.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include <stdio.h>
22
#include <stdlib.h>
3-
#include <math.h>
43

54
void kontrolbit(int *kod, int lengh)
65
{
@@ -36,16 +35,6 @@ void kontrolbit(int *kod, int lengh)
3635
}
3736
}
3837

39-
void tobit(int x, int *byte)
40-
{
41-
for(int i = 0; i < 8; i++)
42-
{
43-
byte[i] = x%2;
44-
x = x/2;
45-
}
46-
invert(byte);
47-
}
48-
4938
void invert(int *byte)
5039
{
5140
int tmp[8], g = 0;
@@ -55,12 +44,22 @@ void invert(int *byte)
5544
byte[i] = tmp[i];
5645
}
5746

47+
void tobit(int x, int *byte)
48+
{
49+
for(int i = 0; i < 8; i++)
50+
{
51+
byte[i] = x%2;
52+
x = x>>1;
53+
}
54+
invert(byte);
55+
}
56+
5857
int todec(int *byte)
5958
{
6059
int x = 0, st = 7;
6160
for(int i = 0; i < 8; i++)
6261
{
63-
x = x + byte[i]*pow(2, st);
62+
x = x + (byte[i]<<st);
6463
st--;
6564
}
6665
return x;

hammingenc.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include <stdio.h>
22
#include <stdlib.h>
3-
#include <math.h>
43

54
void kontrolbit(int *kod, int lengh)
65
{
@@ -26,16 +25,6 @@ void kontrolbit(int *kod, int lengh)
2625
}
2726
}
2827

29-
void tobit(int x, int *byte)
30-
{
31-
for(int i = 0; i < 8; i++)
32-
{
33-
byte[i] = x%2;
34-
x = x/2;
35-
}
36-
invert(byte);
37-
}
38-
3928
void invert(int *byte)
4029
{
4130
int tmp[8], g = 0;
@@ -45,12 +34,22 @@ void invert(int *byte)
4534
byte[i] = tmp[i];
4635
}
4736

37+
void tobit(int x, int *byte)
38+
{
39+
for(int i = 0; i < 8; i++)
40+
{
41+
byte[i] = x%2;
42+
x = x>>1;
43+
}
44+
invert(byte);
45+
}
46+
4847
int todec(int *byte)
4948
{
5049
int x = 0, st = 7;
5150
for(int i = 0; i < 8; i++)
5251
{
53-
x = x + byte[i]*pow(2, st);
52+
x = x + (byte[i]<<st);
5453
st--;
5554
}
5655
return x;

0 commit comments

Comments
 (0)