Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Character to binary

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am trying to write a function in C that takes a single character, converts it to binary, and then reverses the order of bits. I had originally tried it using putchar, but the function is not allowed to print anything, simply return the reversed bits. I am wondering if anyone is able to offer any help, or provide an example of how I could do this, thanks! This must be done with bitwise operations.

**Also if this is not posted in the correct spot please let me know, I wasn't sure where else to post, thanks!**
 
Sheriff
Posts: 15929
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouldn't an "example of how to do this" actually be doing the work for you? People usually post the code they've written and ask for feedback. What have you tried so far?
 
J Hiddleston
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Wouldn't an "example of how to do this" actually be doing the work for you? People usually post the code they've written and ask for feedback. What have you tried so far?



I have tried this, but as I said it was using putchar which prints the line out which is not allowwed.

 
Junilu Lacar
Sheriff
Posts: 15929
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you think of some other way to gather the bits you've calculated into a single value that you can return?
 
Marshal
Posts: 70625
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

J Hiddleston wrote: . . putchar . . .

That prints a char and what you want is to set a particular bit.
 
Rancher
Posts: 234
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi J - without giving a full answer, you need a way of:
  • extracting bits from a number
  • constructing a number from bits

  • One way to extract bit number X from value A is:
    To set bit X in value A (assuming it is currently 0):
    So you could combine these in a loop, extracting a bit from your input value and setting the appropriate bit in your output (reversed bits) value. You just need to work out how to calculate 'appropriate'.

    Does that make sense?
     
    Attractive, successful people love this tiny ad:
    the value of filler advertising in 2020
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic