c programming / binary question

This is a discussion on c programming / binary question within the C Programming forums, part of the General Programming Boards category; is there a simple way to declare a line of zeros and ones as binary code in c? i have ...

  1. #1
    bpf
    bpf is offline
    Registered User
    Join Date
    Aug 2007
    Posts
    7

    c programming / binary question

    is there a simple way to declare a line of zeros and ones as binary code in c? i have written a program that converts integers to binary code but it is a character string. i am trying to write a program that takes two integer inputs, converts them to binary and then calculates the sum.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    What do you mean by converting ints to binary? An int is already in binary format in the computer hardware. If you're referring to the representation of the number in text form, that's something different than what the number actually is.

    Don't get confused into thinking the same number is different if it's in different formats. It's all the same if it's an int.

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    this what you're looking for?

    Code:
    int n = 112314;
    int b = 0;
    for (int j=0; j<16; ++j)
    {
    	b = (n>>j)&0x00000001;
    	printf("&#37;d", b);
    }
    ?

    lol, you know, to add two binary numbers this is what you do;
    http://www.play-hookey.com/digital/adder.html

    so i bet what you want to do is store int as binary values (within an array of int), then write an algorithm to perform the task of an adder?

    then you OR mask (this char, '|') all the array values in to a new int.

    i don't know why you want to go through the trouble to do this conceptual action yourself, but i'm going to assume it's for learning purposes. :-)

    what you might have wanted to say is you want to simulate binary addiction in C, that would clear some confusion for others.
    Last edited by simpleid; 08-29-2007 at 03:56 PM.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    It might be easier to add the two numbers together, and then print the sum out as binary.

    To convert it back the other way, consider
    Code:
    const char *binary = "1010100";
    long number = strtol(binary, NULL, 2);
    where 2 is base 2. (You can supply 0 and strtol(), from <stdlib.h>, tries base 8 (leading 0), base 16 (leading 0x), and base 10.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-25-2006, 03:14 PM
  2. Replies: 0
    Last Post: 11-04-2006, 10:07 AM
  3. binary search and search using binary tree
    By Micko in forum C++ Programming
    Replies: 9
    Last Post: 03-18-2004, 09:18 AM
  4. binary
    By webturtle0 in forum A Brief History of Cprogramming.com
    Replies: 52
    Last Post: 12-05-2002, 01:46 AM
  5. Binary Tree Question
    By tar in forum C Programming
    Replies: 6
    Last Post: 09-26-2002, 01:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21