1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to store a 64 bit integer into 4x16 bit integers ?

Discussion in 'C' started by Thaus015, Feb 7, 2019.

  1. Thaus015

    Thaus015 New Member

    Joined:
    Jun 15, 2017
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Female
    Hello, I need your to code the following logic. I am not expert in programming side but I'm pretty sure its just a matter of some bitwise operations, I'm just not entirely sure of exactly what I should be doing.

    Input of Block is 64 bits, For example A=64
    Output of block should be x =16
    y=16
    z=16
    s=16

    How can this logic can be implemented in C++. Please share the logic.
     
  2. rustyoldguy

    rustyoldguy New Member

    Joined:
    Feb 21, 2020
    Messages:
    2
    Likes Received:
    2
    Trophy Points:
    3
    Gender:
    Male
    Location:
    Germany
    Here a example in cpp, i think it is very easy to change it to c.

    First "lintunion.h"
    Code:
    #ifndef LONGINTUNION_H_INCLUDED
    #define LONGINTUNION_H_INCLUDED
    
    //#include <iostream>
    //#include <iomanip>
    
    #include <stdio.h>
    #include <stdlib.h> // EXIT_SUCCESS
    
    union einheit
     {
      unsigned long ulwert;
      unsigned char mwertbytes[8];
      unsigned short mwertushort[4];
     };
    
     void buzubin(unsigned int letter, char wort[]);
     void zeigeWerte(union einheit ehe);
     void dectoshort( unsigned short zahl, char zudi[], short vornullen);
    
    #endif // LONGINTUNION_H_INCLUDED
    
    
    now lintinion.c
    Code:
    #include "lintunion.h"
    
    /* -----------------------------------------------------------------------
     * buzubin converts a decimal value(only 0 to 255) given by argument 1
     * in to a binary value shown as string. This string is stored in argument 2.
     * The sting shows all eight bit of the value in argument 1.
     * the other bytes is for to terminate the string
     * syntax:  buzubin( char-value_as_int, Binaer_value_as_string);
     */
    
    void buzubin(unsigned int letter, char wort[])
    {
    unsigned int slei, dum, testwert;
     if ((letter > 0) || (letter <= 255))
     {
      /* Ininitalisert die 8 Binärstellen mit Null */
       for (slei = 0; slei <= 11; slei++)
        wort[8] = '\0';
    
     for (slei = 0; slei <= 7; slei++)
      {
       testwert = 128 >> slei;  /* Bit7(128dez) ist auf Char-Nr.0 */
       dum = letter & testwert;
       if (dum != 0)  wort[slei] = '1';
        else          wort[slei] = '0';
       wort[slei + 1] = '\0';
       }
       
     }
    
    }
    
    // shows all values
    void zeigeWerte(union einheit ehe)
    {
     int i;
     char binwort[15] = {0};
     char longwort[100] = {0};
    
     //show values of unsigned char
      printf ("unsigned long value: %lu binary\n", ehe.ulwert);
    
      //std::cout << "unsigned long value: " << ehe.ulwert << "     binary: ";
    
      for (i = 7; i >= 0; i--)
       {
       buzubin(ehe.mwertbytes[i], binwort);
        printf("%s", binwort);
       //std::cout << binwort << " ";
       }
       //std::cout <<std::endl;   
       printf ("\n");
       //std::cout <<"Now as unsigned char: " << std::endl;
       printf ("Now as unsigned char: \n");
     for (i = 7; i >= 0; i--)
      {
       buzubin(ehe.mwertbytes[i], binwort);
       printf("mwertshort[ %2d] = %4u    binary: %s\n", i, (int)ehe.mwertbytes[i], binwort);
       //std::cout << "mwertbytes[" << i << "] = " << std::setw(3) << (int)ehe.mwertbytes[i] << "   binary: " <<  binwort << std::endl;
      }
     //Werte von unsigned char zeigen
     //std::cout << std::endl << "Now as unsigned short: " << std::endl;
     printf("\nNow as unsigned short: ");
     for (i = 3; i >= 0; i--)
      {
       dectoshort( (unsigned long)ehe.mwertushort[i], longwort, 1);
       printf("mwertshort[ %d] = %6u    binary: %s\n", i, (int)ehe.mwertushort[i], longwort);
       //std::cout << "mwertshort[" << i << "] = " << std::setw(6) << (int)ehe.mwertushort[i] << "   binary: " << longwort <<std::endl;
      }
     //std::cout << "---------------------------------------------------------" << std::endl;
     printf("---------------------------------------------------------\n");
    }
    
    /** converts arg 1(unsigned short decimal)
      * in a char-string (argument2), if argument 3(with leading zeros) 0
      * the leading Zerows will not shown in the string, else
      * with leading zeros
      */
    void dectoshort( unsigned short zahl, char zudi[], short vornullen)
    {
    char wort[35] = {0};
    char worz[35] = {0};
    int slei, sleib;
    unsigned short dum2, testwert, kon = 1;
    
    kon <<= 15;
    
    for (slei = 0; slei <= 15; slei++)
      {
       testwert = kon >> slei;  /* Bit30(1073741824dez) is char number 0 */
       dum2 = zahl & testwert;
       if (dum2)  wort[slei] = '1';
        else      wort[slei] = '0';
      }
    // begin to set ende of leading zeros
    for (slei = 0; slei <= 15; slei++)
     if (wort[slei] == '1') break;
    
    // wort is with leading zeros, worz without
     for (sleib = 0; sleib <= 32; sleib++)
            {
             worz[sleib] = wort[slei];
             if (wort[slei] == '\0')
              break;
             slei++;
            }
    // take result in to argument 2
     for (slei = 0; slei <= 15; slei++)
      if ( vornullen == 0)
       zudi[slei] = worz[slei];   // if arg 3(vornullen) = 0 string with no leading zeros
        else
         zudi[slei] = wort[slei]; // if arg 3 = 1 then with leading zeros
    
    }
    
    here main.c
    Code:
    #include "lintunion.h"
    
    
    int main()
    {
     union einheit flint;
    
     printf("test\n");
     //std::cout << "Test" << std::endl;
     printf("length of long int......: %lu byte\n", sizeof(long int));
     //std::cout << "length of long int: " << sizeof(long int) << " byte" << std::endl;
     printf("length of unsigned short: %lu byte\n", sizeof(short));
     //std::cout << "length of short...: " << sizeof(short) << " byte"  << std::endl << std::endl;
    
     flint.ulwert = 1234567890;
    #include "lintunion.h"
    
    
    int main()
    {
     union einheit flint;
    
     printf("test\n");
     //std::cout << "Test" << std::endl;
     printf("length of long int......: %lu byte\n", sizeof(long int));
     //std::cout << "length of long int: " << sizeof(long int) << " byte" << std::endl;
     printf("length of unsigned short: %lu byte\n", sizeof(short));
     //std::cout << "length of short...: " << sizeof(short) << " byte"  << std::endl << std::endl;
    
     flint.ulwert = 1234567890;
    
     //Werte von unsigned char zeigen
     zeigeWerte(flint);
    
     flint.ulwert = 0;
    
     printf("\nNow all variables are deleted:\n");
     //std::cout << std::endl <<"Now all variables are deleted:" << std::endl;
    
     //Werte von unsigned char zeigen
     zeigeWerte(flint);
    
     flint.mwertushort[0] = 59861;
     flint.mwertushort[1] = 17142;
     flint.mwertushort[2] = 9861;
     flint.mwertushort[3] = 7142;
    
     printf("here the short-variables:\n");
     //std::cout << std::endl << "Jetzt Zuweisung von Werten an short-Variable:" << std::endl;
    
     //Werte von unsigned char zeigen
     zeigeWerte(flint);
    
     return EXIT_SUCCESS;
    }
    
     //shows values of unsigned char
     zeigeWerte(flint);
    
     flint.ulwert = 0;
    
     printf("\nNow all variables are deleted:\n");
     //std::cout << std::endl <<"Now all variables are set so zero:" << std::endl;
    
     //Werte von unsigned char zeigen
     zeigeWerte(flint);
    
     flint.mwertushort[0] = 59861;
     flint.mwertushort[1] = 17142;
     flint.mwertushort[2] = 9861;
     flint.mwertushort[3] = 7142;
    
     printf("here the short-variables:\n");
     //std::cout << std::endl << "now the short-variables" << std::endl;
    
     //Werte von unsigned char zeigen
     zeigeWerte(flint);
    
     return EXIT_SUCCESS;
    }
    
    
    It was coded with codelite on SUSE LINUX tumbleweed with gcc.
    Should run on other systens too.
    I got no errors or warnings if i took // instead of /**/ for coments.
    So you can change the code better from c to c++.
    Sorry
    To make the choice easier with
    #ifdef
    i was (first)to lazy:
    Her to change easier from c to cpp_

    at first again lintunion.h:
    Code:
    #ifndef LONGINTUNION_H_INCLUDED
    #define LONGINTUNION_H_INCLUDED
    
    /** to change from c to cpp:
        //#define IS_C  1
        #define IS_CPP 2
    
     */
    
    /** Version for c */
    #define IS_C  1
    //#define IS_CPP 2
    
    #ifdef IS_CPP
    #include <iostream>
    #include <iomanip>
    #endif
    
    #ifdef IS_C
    #include <stdio.h>
    #include <stdlib.h> // EXIT_SUCCESS
    #endif
    
    union einheit
     {
      unsigned long ulwert;
      unsigned char mwertbytes[8];
      unsigned short mwertushort[4];
     };
    
     void buzubin(unsigned int letter, char wort[]);
     void zeigeWerte(union einheit ehe);
     void dectoshort( unsigned short zahl, char zudi[], short vornullen);
    
    #endif // LONGINTUNION_H_INCLUDED
    
    
    
    for change to cpp you must take an other name for lintunion.c; lintunion.cpp
    Code:
    #include "lintunion.h"
    
    /* -----------------------------------------------------------------------
     * buzubin converts a decimal value(only 0 to 255) given by argument 1
     * in to a binary value shown as string. This string is stored in argument 2.
     * The sting shows all eight bit of the value in argument 1.
     * the other bytes is for to terminate the string
     * syntaxc:  buzubin( char-value_as_int, Binaer_value_as_string);
     */
    
    void buzubin(unsigned int letter, char wort[])
    {
    unsigned int slei, dum, testwert;
     if ((letter > 0) || (letter <= 255))
     {
      /* Ininitalisert die 8 Binärstellen mit Null */
       for (slei = 0; slei <= 11; slei++)
        wort[8] = '\0';
    
     for (slei = 0; slei <= 7; slei++)
      {
       testwert = 128 >> slei;  /* Bit7(128dez) ist auf Char-Nr.0 */
       dum = letter & testwert;
       if (dum != 0)  wort[slei] = '1';
        else          wort[slei] = '0';
       wort[slei + 1] = '\0';
       }
       
     }
    
    }
    
    // shows all values
    void zeigeWerte(union einheit ehe)
    {
     int i;
     char binwort[15] = {0};
     char longwort[100] = {0};
    
    
     //show values of unsigned char
      #ifdef IS_C
          printf ("unsigned long value: %lu binary\n", ehe.ulwert);
      #endif
     
       #ifdef IS_CPP
         std::cout << "unsigned long value: " << ehe.ulwert << "     binary: ";
       #endif
    
    
     
    
      for (i = 7; i >= 0; i--)
       {
       buzubin(ehe.mwertbytes[i], binwort);
        #ifdef IS_C
         printf("%s", binwort);
       #endif
       
       #ifdef IS_CPP
           std::cout << binwort << " ";
        #endif
           
       }
     
         #ifdef IS_CPP
          std::cout <<std::endl << "Now as unsigned char: " << std::endl;
        #endif
       #ifdef IS_C
         printf ("\nNow as unsigned char: \n");
       #endif
     for (i = 7; i >= 0; i--)
      {
       buzubin(ehe.mwertbytes[i], binwort);
       #ifdef IS_C
         printf("mwertshort[ %2d] = %4u    binary: %s\n", i, (int)ehe.mwertbytes[i], binwort);
       #endif
       #ifdef IS_CPP
         std::cout << "mwertbytes[" << i << "] = " << std::setw(3) << (int)ehe.mwertbytes[i] << "   binary: " <<  binwort << std::endl;
       #endif
      }
     //Werte von unsigned char zeigen
     #ifdef IS_CPP
        std::cout << std::endl << "Now as unsigned short: " << std::endl;
     #endif
     printf("\nNow as unsigned short: ");
     for (i = 3; i >= 0; i--)
      {
       dectoshort( (unsigned long)ehe.mwertushort[i], longwort, 1);
       #ifdef IS_C
          printf("mwertshort[ %d] = %6u    binary: %s\n", i, (int)ehe.mwertushort[i], longwort);
       #endif
       #ifdef IS_CPP
         std::cout << "mwertshort[" << i << "] = " << std::setw(6) << (int)ehe.mwertushort[i] << "   binary: " << longwort <<std::endl;
       #endif
      }
     #ifdef IS_CPP
       std::cout << "---------------------------------------------------------" << std::endl;
     #endif
    #ifdef IS_C
     printf("---------------------------------------------------------\n");
    #endif
    }
    
    /** converts arg 1(unsigned short decimal)
      * in a char-string (argument2), if argument 3(with leading zeros) 0
      * the leading Zerows will not shown in the string, else
      * with leading zeros
      */
    void dectoshort( unsigned short zahl, char zudi[], short vornullen)
    {
    char wort[35] = {0};
    char worz[35] = {0};
    int slei, sleib;
    unsigned short dum2, testwert, kon = 1;
    
    kon <<= 15;
    
    for (slei = 0; slei <= 15; slei++)
      {
       testwert = kon >> slei;  /* Bit30(1073741824dez) is char number 0 */
       dum2 = zahl & testwert;
       if (dum2)  wort[slei] = '1';
        else      wort[slei] = '0';
      }
    // begin to set ende of leading zeros
    for (slei = 0; slei <= 15; slei++)
     if (wort[slei] == '1') break;
    
    // wort is with leading zeros, worz without
     for (sleib = 0; sleib <= 32; sleib++)
            {
             worz[sleib] = wort[slei];
             if (wort[slei] == '\0')
              break;
             slei++;
            }
    // take result in to argument 2
     for (slei = 0; slei <= 15; slei++)
      if ( vornullen == 0)
       zudi[slei] = worz[slei];   // if arg 3(vornullen) = 0 string with no leading zeros
        else
         zudi[slei] = wort[slei]; // if arg 3 = 1 then with leading zeros
    
    }
    
    the same for main.c, take main.cpp
    Code:
    #include "lintunion.h"
    
    
    int main()
    {
     union einheit flint;
     #ifdef IS_C
     printf("test\n");
     printf("length of long int......: %lu byte\n", sizeof(long int));
     printf("length of unsigned short: %lu byte\n", sizeof(short));
     #endif
     #ifdef IS_CPP
       std::cout << "Test" << std::endl;
       std::cout << "length of long int: " << sizeof(long int) << " byte" << std::endl;
       std::cout << "length of short...: " << sizeof(short) << " byte"  << std::endl << std::endl;
     #endif
    
     flint.ulwert = 1234567890;
    
     //Werte von unsigned char zeigen
     zeigeWerte(flint);
    
     flint.ulwert = 0;
     #ifdef IS_C
     printf("\nNow all variables are deleted:\n");
     #endif
    
     #ifdef IS_CPP
       std::cout << std::endl <<"Now all variables are deleted:" << std::endl;
     #endif
     //Werte von unsigned char zeigen
     zeigeWerte(flint);
    
     flint.mwertushort[0] = 59861;
     flint.mwertushort[1] = 17142;
     flint.mwertushort[2] = 9861;
     flint.mwertushort[3] = 7142;
    
     #ifdef IS_C
       printf("here the short-variables:\n");
     #endif
     #ifdef IS_CPP
       std::cout << std::endl << "Jetzt Zuweisung von Werten an short-Variable:" << std::endl;
     #endif
     //Werte von unsigned char zeigen
     zeigeWerte(flint);
    
     return EXIT_SUCCESS;
    }
    
    Hope, this example is not to dificult, and it is useful.
     
    shabbir likes this.

Share This Page