![]() |
| | #1 |
| Registered User Join Date: Feb 2009
Posts: 2
| Using the pack function I have written a c program that prompts the user for integers 1 and 2 and then prints out that number in different way. Here it is Code: #include "stdafx.h"
#include <stdio.h>
void main () {
char c;
int i, n1, n2, number;
long k;
float f;
while () {
printf ("enter first number: ");
scanf("d%", &n1);
printf ("d%", &n1);
printf ("enter last number: ");
scanf("d%", &n2);
printf ("d%", &n2);
for (number=n1; number <=n2; number++) {
c = number;
i = number;
k = number;
f = number;
printf("char: (dec) = \t%12d (uns dec) = %12u (hex) = %12x\n",c,c,c);
printf("int: (dec) = \t%12d (uns dec) = %12u (hex) = %12x\n",i,i,i);
printf("char: (dec) = \t%12ld (uns dec) = %12lu (hex) = %12lx\n",k,k,k);
printf("f: (float) = \t %12.3f\n\n\n", f);
}
}
}
pack (0, 80) = 128 pack (0,ff) = 255 I am not sure how i can do this nor do i know how to use the pack function. Can someone help me please? Help me please! |
| tbrown81 is offline | |
| | #2 |
| Registered User Join Date: Oct 2008 Location: TX
Posts: 1,262
| What happens if input variables a and b are 32 bit ints? Are they discarded or are they cast and packed? If a=0x10 and b=0x80 are packed together into a 16 bit space, what is the value printed out then? |
| itCbitC is offline | |
| | #3 |
| Registered User Join Date: Feb 2009
Posts: 2
| no you can only input two 8 bit values for a and b. for instance: pack (ff,01) = -255 pack (7f,ff) = 32767 pack (80,00) = -32768 Pack (a,b) and the inputs a and be will be 2 hex for a and 2 for b. So the input can only be a 16 bit input never a 32 bit in my case. |
| tbrown81 is offline | |
| | #4 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Well, to give a big hint: If I gave you the numbers 4 and 7, how could you combine them to get 47 using arithmetic? |
| tabstop is offline | |
| | #5 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
|
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is offline | |
| | #6 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| |
| tabstop is offline | |
| | #7 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| How 'bout this? I'm not sure if I understood the point but I think I did: Code: #include <stdio.h>
#include <linux/types.h>
typedef __s16 dword;
struct input {
char A;
char B;
};
union twobytes {
struct input this;
dword that;
};
dword pack (char a, char b) {
union twobytes example;
example.this.A=a;
example.this.B=b;
return example.that;
}
int main() {
char a=0, b=1;
printf("%d\n",pack(a,b));
return 0;
}
ps. Is there a more portable source of specific bitsized datatypes than this? I just dug it out of "usr/include" because I didn't want to rely on short.
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS Last edited by MK27; 02-20-2009 at 11:33 AM. |
| MK27 is offline | |
| | #8 | |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Quote:
If you're willing to put up with C99, you have all the types in stdint.h. | |
| tabstop is offline | |
| | #9 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| Bitshifting is multiplication, right? Code: int pack (unsigned char a, unsigned char b) {
int A=a<<8;
return A+b;
}
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is offline | |
| | #10 |
| Registered User Join Date: Oct 2008 Location: TX
Posts: 1,262
| |
| itCbitC is offline | |
| | #11 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| I was going by this from the OP: pack (0, 80) = 128 pack (0,ff) = 255
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is offline | |
| | #12 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| If you mean "correct period", then I would agree with you. There is no endianness involved in bitshifting, and so there can be no endianness problems in the above. (Remember "<<" doesn't mean "shift to a lower address", it means "shift to more significant bits".) |
| tabstop is offline | |
| | #13 |
| Registered User Join Date: Oct 2008 Location: TX
Posts: 1,262
| My ooops!, I noticed that right after I posted it; the OS routine will load the low byte into the low(high) address depending on the endiannes. |
| itCbitC is offline | |
| | #14 |
| Registered User Join Date: Oct 2008 Location: TX
Posts: 1,262
| |
| itCbitC is offline | |
| | #15 |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,381
| We're setting a really low bar for what "arithmetic" means, aren't we?
__________________ "Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot |
| brewbuck is offline | |
![]() |
| Tags |
| c programming, pack function |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Compiling sample DarkGDK Program | Phyxashun | Game Programming | 6 | 01-27-2009 03:07 AM |
| Seg Fault in Compare Function | tytelizgal | C Programming | 1 | 10-25-2008 03:06 PM |
| Another syntax error | caldeira | C Programming | 31 | 09-05-2008 01:01 AM |
| How to fix misaligned assignment statements in the source code? | biggyK | C++ Programming | 28 | 07-16-2006 11:35 PM |
| Dikumud | maxorator | C++ Programming | 1 | 10-01-2005 06:39 AM |