Jump to content

Hey guys, I am supposed to create a program that uses strtol() or strtoul() to do, but I have no idea where to start.The program is supposed to take 3 integers and turn on the corresponding bits in an unsigned short to output a result that shows set membership. I'm supposed to use bitwise operations for a majority of the work but I'm not sure what to even do. I'm also not entirely sure how to take the 3 numbers as one input and then split into three variables so that would help tremendously. 

EX: 2 3 4 would return 0000 0000 0001 1100

 

I am also supposed to have another program that takes two hex 32 bit integers and displays which bits the two numbers have set to one.

EX: 0xE0C874A3 0xD49157FF would return
Bits 0,1,5,7,10,12,14,23,30,31, in common. Hexadecimal: c08054a3, Signed: -1065331549, Unsigned: 3229635747

i5 4670k| Asrock H81M-ITX| EVGA Nex 650g| WD Black 500Gb| H100 with SP120s| ASUS Matrix 7970 Platinum (just sold)| Patriot Venom 1600Mhz 8Gb| Bitfenix Prodigy. Build log in progress 

Build Log here: http://linustechtips.com/main/topic/119926-yin-yang-prodigy-update-2-26-14/

Link to comment
https://linustechtips.com/topic/850332-using-strtol-and-strtoul/
Share on other sites

Link to post
Share on other sites

17 hours ago, Unimportant said:

@CJPowell27 Language? I'd guess C but would help to be sure.

Oops, it is C

i5 4670k| Asrock H81M-ITX| EVGA Nex 650g| WD Black 500Gb| H100 with SP120s| ASUS Matrix 7970 Platinum (just sold)| Patriot Venom 1600Mhz 8Gb| Bitfenix Prodigy. Build log in progress 

Build Log here: http://linustechtips.com/main/topic/119926-yin-yang-prodigy-update-2-26-14/

Link to post
Share on other sites

#include <stdio.h>
#include <memory.h>
#include <stdlib.h>

#define MAX_ITEMS 255
#define MAX_ITEM_BUFFER_LENGTH 4096
#define DELIMITER ' '
#define NUM_BITS(x) (sizeof(x)*__CHAR_BIT__)

size_t split(char *str, char **out, char delimiter, size_t maximumItems, size_t maximumItemLength)
{
    char splitString[2];
    size_t outputCount = 0;

    splitString[0] = delimiter;
    splitString[1] = '\0';
    char *token = strtok(str, splitString);
    while ( (token != NULL) && (outputCount < maximumItems) ) {
        strncpy(out[outputCount++], token, maximumItemLength);
        token = strtok(NULL, splitString);
    }
    return outputCount;
}

void printBinaryLSB(unsigned int number)
{
    for (int i = 0; i < NUM_BITS(number); i++) {
        unsigned int targetBit = (1U << (NUM_BITS(number) - i - 1));
        if (number & targetBit) {
            printf("%c", '1');
        } else {
            printf("%c", '0');
        }
    }
    printf(": %u\n", number);
}

void printBinaryMSB(unsigned int number)
{
    for (int i = 0; i < NUM_BITS(number); i++) {
        unsigned int targetBit = (1U <<  i);
        if (number & targetBit) {
            printf("%c", '1');
        } else {
            printf("%c", '0');
        }
    }
    printf(": %u\n", number);
}

int main(int argc, char *argv)
{
    //Initialization
    char testString[] = "55 22 1 0 1024 56 65535 24000000";

    //Allocate memory for the split container
    //2D character array (AKA 1D string aray)
    char **splitContainer = calloc(MAX_ITEMS, sizeof(char *));
    for (int i = 0; i < MAX_ITEMS; i++) {
        splitContainer[i] = calloc(MAX_ITEM_BUFFER_LENGTH, sizeof(char));
    }

    //Split the string into tokens
    size_t numberOfTokens = split(testString, splitContainer, DELIMITER, MAX_ITEMS, MAX_ITEM_BUFFER_LENGTH);

    //Iterate through tokens and print the binary representation
    for (int i = 0; i < numberOfTokens; i++) {
        unsigned int targetNumber = (int) strtoul(splitContainer[i], NULL, 10); //Assume base 10
        printBinaryLSB(targetNumber);
        //printBinaryLSB(1)
        //00000000000000000000000000000001: 1

        //printBinaryMSB(1)
        //10000000000000000000000000000000: 1
    }


    //Cleanup allocated memory
    for (int i = 0; i < MAX_ITEMS; i++) {
        free(splitContainer[i]);
    }
    free(splitContainer);

    return 0;
}

This should get you started. On my machine, prints

00000000000000000000000000110111: 55
00000000000000000000000000010110: 22
00000000000000000000000000000001: 1
00000000000000000000000000000000: 0
00000000000000000000010000000000: 1024
00000000000000000000000000111000: 56
00000000000000001111111111111111: 65535
00000001011011100011011000000000: 24000000

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×