binary questions

This is a discussion on binary questions within the C Programming forums, part of the General Programming Boards category; Hi, At http://www.cs.cf.ac.uk/Dave/C I am studying C and I also (try to) make the exercises. I can't solve this one: ...

  1. #1
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312

    binary questions

    Hi,

    At http://www.cs.cf.ac.uk/Dave/C I am studying C and I also (try to) make the exercises. I can't solve this one: http://www.cs.cf.ac.uk/Dave/C/node13.html#ex:bin

    Exercise 12507

    Write a function that prints out an 8-bit (unsigned char) number in binary format.
    I tried something:

    Code:
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
    	unsigned char x;
    	x = 89;
    	
    	printf("%d", x);
    
    	return 0;
    }
    Actually, I don't have a clue how to do this
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    C doesn't have a way of printing binary values. You have to do some conversions first. If you want to do it manually, you'll end up using bitwise operators to separate the value into bits so that you can print them individually.
    My best code is written with the delete key.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    %b for binary formatting in printf was deliberately left out so that students would have an easy homework question to solve
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    After some "googling" I found this site and coded the solution:

    Code:
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
    	unsigned char x; // 8 bits
    	x = 69;
    	
    	int count;
    	for(count = 8; count > 0; --count)
    	{
    		printf("%d", (x & 128) ? 1 : 0);
    		x <<= 1;
    	}
    
    	return 0;
    }
    And after some experimenting I now finally understand "binary stuff" much better
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  5. #5
    Registered User
    Join Date
    Jun 2007
    Location
    Lees Summit Missouri
    Posts
    11
    It is no surprise I like Ideswa . He likes binary,uses gcc and ubuntu !
    I suppose I'm just biased that way.

  6. #6
    Registered User
    Join Date
    Jun 2007
    Location
    Rome, NY
    Posts
    24
    Quote Originally Posted by Salem View Post
    %b for binary formatting in printf was deliberately left out so that students would have an easy homework question to solve
    Then was the creat() function designed just to show CS students that a lot of things in CS don't make sense, like writing the word 'create' without the 'e'.

    Har har har

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    They had already used create for other things, and leet speak hadn't caught on yet, so they couldn't use creat3.
    My best code is written with the delete key.

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    I used to think &#37;b would have been a good format specifier for printf() and scanf(). Now I seem to think it less necessary, but it still will would have been nice.

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by MacGyver View Post
    I used to think %b would have been a good format specifier for printf() and scanf(). Now I seem to think it less necessary, but it still will would have been nice.
    Honestly though, I've NEVER had a need to display a value in binary, outside of some school assignment. The close relationship between binary and hex almost always makes hex a better choice anyway.

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    I agree with the hex-binary relationship. Once I got used to hex, I decided it made a lot more sense to use than straight binary, especially for larger binary values (32-bit and now 64-bit).

    Even with that said, I still think it's odd that we have decimal, octal, hexadecimal, and yet no binary. Let's just throw away octal and put binary in its place.

    /me waits for a bunch of angry octal fans to throw a fit.

  11. #11
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by honorable_sir View Post
    It is no surprise I like Ideswa . He likes binary,uses gcc and ubuntu !
    I suppose I'm just biased that way.
    Yes, he's misguided too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 06:20 AM
  2. best STL method to implement a binary tree
    By MatthewDoucette in forum C++ Programming
    Replies: 8
    Last Post: 06-16-2006, 08:08 AM
  3. Print file in binary mode
    By vikernes in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2006, 12:43 AM
  4. Binary Trees
    By wvu2005 in forum C Programming
    Replies: 7
    Last Post: 10-15-2005, 05:59 PM
  5. Reading/Writing in Binary
    By kuphryn in forum C++ Programming
    Replies: 4
    Last Post: 12-11-2001, 10:32 PM

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