Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- int checkRange(const char * varName, int x, int low, int high) {
- if (x < low || x > high) {
- printf("Out of range %s=%d, [%d-%d]\n", varName, x, low, high);
- return 1;
- }
- return 0;
- }
- void testPack (int a, int b, int c, int aMax, int bMax, int cMax) {
- aMax += 1; // Increment the maximums to allow for the entire range (e.g. a max of 9 is 10 values if 0 is included).
- bMax += 1;
- cMax += 1;
- long pack = ((a * bMax) + b) * cMax + c;
- printf("Packed value: %d (0x%x)\n", pack, pack);
- long wrk = pack;
- int cUnpack = wrk % cMax;
- wrk = wrk / cMax;
- int bUnpack = wrk % bMax;
- wrk = wrk / bMax;
- int aUnpack = wrk;
- printf("Unpacked:\na=%d, b=%d, c=%d\n", aUnpack, bUnpack, cUnpack);
- }
- int main (int argc, char * argv[]) {
- printf("Encoder 20 bit.\n");
- if (argc != 4) {
- printf("Expected 3 numeric arguments\n");
- printf("%s a b c\n", argv[0]);
- printf(" a: [0-40]\n");
- printf(" b: [0-41]\n");
- printf(" c: [0-443]\n");
- exit(1);
- }
- int a = atoi(argv[1]);
- int b = atoi(argv[2]);
- int c = atoi(argv[3]);
- printf("a=%d, b=%d, c= %d\n", a, b, c);
- int err = checkRange("a", a, 0, 40);
- err |= checkRange("b", b, 0, 41);
- err |= checkRange("c", c, 0, 443);
- if (err) {
- exit(1);
- }
- testPack (a, b, c, 40, 41, 443);
- exit(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement