Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- three ways to find out the byte-size of four C types, without sizeof()
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <string.h>
- /************************************** SHIFT **************************************/
- #define count(x,y) for(x=1; x; x <<= 1) y++
- void verify_bits_count() {
- char chr_init = 0;
- short sht_init = 0;
- int int_init = 0;
- long lng_init = 0;
- short chr_ret = 0, sht_ret = 0, int_ret = 0, lng_ret = 0;
- count(chr_init, chr_ret);
- count(sht_init, sht_ret);
- count(int_init, int_ret);
- count(lng_init, lng_ret);
- printf("\n\nbit count\n\n");
- printf("char has %d bytes\n",chr_ret/8);
- printf("short has %d bytes\n",sht_ret/8);
- printf("int has %d bytes\n",int_ret/8);
- printf("long has %d bytes\n",lng_ret/8);
- }
- /************************************** LOOP **************************************/
- short loop_test(char *chr_char) {
- short chr_val = 0;
- int chr_ret = 0;
- for (chr_val = 0; chr_val <= 100; chr_val++)
- chr_ret += (chr_char[chr_val] == -1 ? 1 : 0);
- return chr_ret;
- }
- #define loop_tst(cc,cv,cr) for (cv = 0; cv <= 100; cv++) cr += (cc[cv] == -1 ? 1 : 0);
- void verify_bits_loop() {
- char *dst = (char *)malloc(100);
- short chr_ret = 0;
- short sht_ret = 0;
- short int_ret = 0;
- short lng_ret = 0;
- char chr_val = 0xFFFFFFFFFFFFFFFF;
- memset(dst, 0x00, 100);
- strcpy(dst, (char *)&chr_val);
- loop_tst(dst,chr_val,chr_ret);
- short sht_val = 0xFFFFFFFFFFFFFFFF;
- memset(dst, 0x00, 100);
- strcpy(dst, (char *)&sht_val);
- loop_tst(dst,sht_val,sht_ret);
- int int_val = (int)0xFFFFFFFFFFFFFFFF;
- memset(dst, 0x00, 100);
- strcpy(dst, (char *)&int_val);
- loop_tst(dst,int_val,int_ret);
- long lng_val = 0xFFFFFFFFFFFFFFFF;
- memset(dst, 0x00, 100);
- strcpy(dst, (char *)&lng_val);
- loop_tst(dst, lng_val, lng_ret);
- printf("\n\nloop test\n\n");
- printf("char has %d bytes\n",chr_ret);
- printf("short has %d bytes\n",sht_ret);
- printf("int has %d bytes\n",int_ret);
- printf("long has %d bytes\n",lng_ret);
- }
- /************************************** STRUCT **************************************/
- #define d_bitsize_init(var) ( (int) (&var+1) - (int) &var) /* this macro not written by me but included for the sake of demonstration as a makeshift "sizeof" */
- struct var_tst {
- uint8_t t_char;
- uint16_t t_shrt __attribute__((__packed__));
- uint32_t t_uint __attribute__((__packed__));
- uint64_t t_long __attribute__((__packed__));
- };
- void verify_bits_struct() {
- struct var_tst *comp = calloc(1,d_bitsize_init(comp));
- short sc = d_bitsize_init(*comp);
- char dst[sizeof(*comp)];
- memset(&comp->t_char, 0x0, (int)d_bitsize_init(comp->t_char));
- memset(&comp->t_shrt, 0x1, (int)d_bitsize_init(comp->t_shrt));
- memset(&comp->t_uint, 0x2, (int)d_bitsize_init(comp->t_uint));
- memset(&comp->t_long, 0x3, (int)d_bitsize_init(comp->t_long));
- memcpy(dst, comp, sizeof(*comp));
- short lcnt = 0, icnt = 0, scnt = 0, ccnt = 0;
- for (;sc >= 0;sc--) {
- switch (dst[sc] ) {
- case 0x0: ccnt++; break;
- case 0x1: scnt++; break;
- case 0x2: icnt++; break;
- case 0x3: lcnt++; break;
- }
- }
- printf("\n\nstruct packing\n\n");
- printf("char has %d bytes\n",ccnt);
- printf("short has %d bytes\n",scnt);
- printf("int has %d bytes\n",icnt);
- printf("long has %d bytes\n",lcnt);
- }
- /************************************** MAIN **************************************/
- int main (int argc, const char * argv[]){
- verify_bits_loop();
- verify_bits_struct();
- verify_bits_count();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment