Skip to main content
deleted 38 characters in body
Source Link
RJM
  • 218
  • 3
  • 11

Assumes little endian address ordering Void*Void* removed

Assumes little endian address ordering Void* removed

Void* removed

added 2 characters in body
Source Link
RJM
  • 218
  • 3
  • 11
#include <stdio.h>

void get_bits(unsigned long long* num, char * out, int bytes);

int main(void)
{
    long long x = 0;

    printf("Enter a number: ");
    scanf("%i""%lli", &x);

    char bits[sizeof(unsigned long long)+1] = {0};
    get_bits(&x, bits, 4);

    printf("%d in binary %s\n", x, bits);

    return 0;
}

//assumes char array of length 1 greater than 
//number of bits
//EDIT: VERSION WITHOUT MEMCPY AS REMINDED BY @REINDERIEN
//REMOVED aliasing issue
void get_bits(unsigned long long * num, char *out, int bytes)
{
    unsigned long long filter = 0x8000000000000000;
   // unsigned long long *temp = num;

    if(bytes <= 0) return;
    if(bytes > 8) bytes = 8;

    filter = filter >> (8*(sizeof(unsigned long long)-bytes));
    //memcpy(&temp, num, bytes);
    int bits = 8*bytes;
    for(int i=0;i<bits;i++) {
        //if(filter & temp)
        //if((filter >> i) & *temp)
        if((filter >> i) & *num)
            out[i] = '1';
        else
            out[i] = '0';

        //temp = temp << 1;
    }
    out[bits] = '\0';
}
#include <stdio.h>

void get_bits(unsigned long long* num, char * out, int bytes);

int main(void)
{
    long long x = 0;

    printf("Enter a number: ");
    scanf("%i", &x);

    char bits[sizeof(unsigned long long)+1] = {0};
    get_bits(&x, bits, 4);

    printf("%d in binary %s\n", x, bits);

    return 0;
}

//assumes char array of length 1 greater than 
//number of bits
//EDIT: VERSION WITHOUT MEMCPY AS REMINDED BY @REINDERIEN
//REMOVED aliasing issue
void get_bits(unsigned long long * num, char *out, int bytes)
{
    unsigned long long filter = 0x8000000000000000;
   // unsigned long long *temp = num;

    if(bytes <= 0) return;
    if(bytes > 8) bytes = 8;

    filter = filter >> (8*(sizeof(unsigned long long)-bytes));
    //memcpy(&temp, num, bytes);
    int bits = 8*bytes;
    for(int i=0;i<bits;i++) {
        //if(filter & temp)
        //if((filter >> i) & *temp)
        if((filter >> i) & *num)
            out[i] = '1';
        else
            out[i] = '0';

        //temp = temp << 1;
    }
    out[bits] = '\0';
}
#include <stdio.h>

void get_bits(unsigned long long* num, char * out, int bytes);

int main(void)
{
    long long x = 0;

    printf("Enter a number: ");
    scanf("%lli", &x);

    char bits[sizeof(unsigned long long)+1] = {0};
    get_bits(&x, bits, 4);

    printf("%d in binary %s\n", x, bits);

    return 0;
}

//assumes char array of length 1 greater than 
//number of bits
//EDIT: VERSION WITHOUT MEMCPY AS REMINDED BY @REINDERIEN
//REMOVED aliasing issue
void get_bits(unsigned long long * num, char *out, int bytes)
{
    unsigned long long filter = 0x8000000000000000;
   // unsigned long long *temp = num;

    if(bytes <= 0) return;
    if(bytes > 8) bytes = 8;

    filter = filter >> (8*(sizeof(unsigned long long)-bytes));
    //memcpy(&temp, num, bytes);
    int bits = 8*bytes;
    for(int i=0;i<bits;i++) {
        //if(filter & temp)
        //if((filter >> i) & *temp)
        if((filter >> i) & *num)
            out[i] = '1';
        else
            out[i] = '0';

        //temp = temp << 1;
    }
    out[bits] = '\0';
}
added 13 characters in body
Source Link
RJM
  • 218
  • 3
  • 11
#include <stdio.h>

void get_bits(unsigned long long* num, char * out, int bytes);

int main(void)
{
    long long x = 0;

    printf("Enter ana integernumber: ");
    scanf("%i", &x);

    char bits[sizeof(intunsigned long long)+1] = {0};
    get_bits(&x, bits, 4);

    printf("%d in binary %s\n", x, bits);

    return 0;
}

//assumes char array of length 1 greater than 
//number of bits
//EDIT: VERSION WITHOUT MEMCPY AS REMINDED BY @REINDERIEN
//REMOVED aliasing issue
void get_bits(unsigned long long * num, char *out, int bytes)
{
    unsigned long long filter = 0x8000000000000000;
   // unsigned long long *temp = num;

    if(bytes <= 0) return;
    if(bytes > 8) bytes = 8;

    filter = filter >> (8*(sizeof(unsigned long long)-bytes));
    //memcpy(&temp, num, bytes);
    int bits = 8*bytes;
    for(int i=0;i<bits;i++) {
        //if(filter & temp)
        //if((filter >> i) & *temp)
        if((filter >> i) & *num)
            out[i] = '1';
        else
            out[i] = '0';

        //temp = temp << 1;
    }
    out[bits] = '\0';
}
#include <stdio.h>

void get_bits(unsigned long long* num, char * out, int bytes);

int main(void)
{
    long long x = 0;

    printf("Enter an integer: ");
    scanf("%i", &x);

    char bits[sizeof(int)+1] = {0};
    get_bits(&x, bits, 4);

    printf("%d in binary %s\n", x, bits);

    return 0;
}

//assumes char array of length 1 greater than 
//number of bits
//EDIT: VERSION WITHOUT MEMCPY AS REMINDED BY @REINDERIEN
//REMOVED aliasing issue
void get_bits(unsigned long long * num, char *out, int bytes)
{
    unsigned long long filter = 0x8000000000000000;
   // unsigned long long *temp = num;

    if(bytes <= 0) return;
    if(bytes > 8) bytes = 8;

    filter = filter >> (8*(sizeof(unsigned long long)-bytes));
    //memcpy(&temp, num, bytes);
    int bits = 8*bytes;
    for(int i=0;i<bits;i++) {
        //if(filter & temp)
        //if((filter >> i) & *temp)
        if((filter >> i) & *num)
            out[i] = '1';
        else
            out[i] = '0';

        //temp = temp << 1;
    }
    out[bits] = '\0';
}
#include <stdio.h>

void get_bits(unsigned long long* num, char * out, int bytes);

int main(void)
{
    long long x = 0;

    printf("Enter a number: ");
    scanf("%i", &x);

    char bits[sizeof(unsigned long long)+1] = {0};
    get_bits(&x, bits, 4);

    printf("%d in binary %s\n", x, bits);

    return 0;
}

//assumes char array of length 1 greater than 
//number of bits
//EDIT: VERSION WITHOUT MEMCPY AS REMINDED BY @REINDERIEN
//REMOVED aliasing issue
void get_bits(unsigned long long * num, char *out, int bytes)
{
    unsigned long long filter = 0x8000000000000000;
   // unsigned long long *temp = num;

    if(bytes <= 0) return;
    if(bytes > 8) bytes = 8;

    filter = filter >> (8*(sizeof(unsigned long long)-bytes));
    //memcpy(&temp, num, bytes);
    int bits = 8*bytes;
    for(int i=0;i<bits;i++) {
        //if(filter & temp)
        //if((filter >> i) & *temp)
        if((filter >> i) & *num)
            out[i] = '1';
        else
            out[i] = '0';

        //temp = temp << 1;
    }
    out[bits] = '\0';
}
deleted 214 characters in body
Source Link
RJM
  • 218
  • 3
  • 11
Loading
added 141 characters in body
Source Link
RJM
  • 218
  • 3
  • 11
Loading
added 141 characters in body
Source Link
RJM
  • 218
  • 3
  • 11
Loading
added 826 characters in body
Source Link
RJM
  • 218
  • 3
  • 11
Loading
added 826 characters in body
Source Link
RJM
  • 218
  • 3
  • 11
Loading
added 1 character in body
Source Link
RJM
  • 218
  • 3
  • 11
Loading
deleted 1 character in body
Source Link
RJM
  • 218
  • 3
  • 11
Loading
added 939 characters in body
Source Link
RJM
  • 218
  • 3
  • 11
Loading
added 939 characters in body
Source Link
RJM
  • 218
  • 3
  • 11
Loading
indent by 4 spaces to pre-format all lines as code - for more markdown info, refer to https://stackoverflow.com/editing-help#code
Source Link
Loading
added 2 characters in body; deleted 10 characters in body
Source Link
RJM
  • 218
  • 3
  • 11
Loading
added 2 characters in body; added 4 characters in body; deleted 4 characters in body; added 4 characters in body
Source Link
RJM
  • 218
  • 3
  • 11
Loading
Source Link
RJM
  • 218
  • 3
  • 11
Loading