?
Solved

Newbie C ? need help with Concept of Bitwise Ops

Posted on 2003-02-24
9
Medium Priority
?
180 Views
Last Modified: 2010-04-15
I have a signed int and I need to place it in a char array big enough so I can put my integer in binary onto the array including '/0'. Now I know I am supposed to use a << or >> to place each piece of the puzzle into each array. I am having a hard time doing this. This is a typical itob(char[], int n). *sigh* I'll be up all night :) I got advice to use a for loop and kick out binary numebrs as I go but I haven't been taught well in class and finding this a hard ordeal.
0
Comment
Question by:javellan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 8

Accepted Solution

by:
akshayxx earned 140 total points
ID: 8014866
no its very simple to do this
char intstr[32];
int x=2123131;

sprintf(intstr,"%d",x);

now printf ur intstr
printf(intstr,"%d =(as string)=> %s \n",x,intstr);
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 8014878
I think the Q is not clear to me!
Are you trying to put each byte of your int into different locations in a char array?
Please give an example and explain.

-Narendra
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8014896
char intstr[32]; size of array shud be good enough to hold ..
and while doing sprintf take care if u need signed or unsigned ..
if in above case number is -1234 .. then the string intstr willl look like
"-1234"

so for use with 'unsigned int x'.. in sprintf .. use
sprintf(intstr,"%u", x)..
in case of negative numbers what u can do is.. to avoid - sign to be held in the array
take the absolute value of the number before sprintf.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 8

Expert Comment

by:akshayxx
ID: 8014998
ohh i didnt see the word "binary "
here is what u r looking for..

#define nBITS 8*sizeof(int)

void getB(unsigned int x,char s[nBITS+1]){
 int i=nBITS;
while(i-->0){
        s[nBITS-i-1]=(x&(1<<i))?'1':'0';
}
s[nBITS]='\0';
}

use it like this

char s[nBITS+1];
unsigned int x=55;
getB(x,s);
printf("%s\n",s);

0
 

Author Comment

by:javellan
ID: 8015465
Okay yes..Im sorry it needs to go into BINARY string.
Akshayxx could you provide a small explanation of the statements you used? I under stand what ? : are but if you coudl break it down that owuld be great, also this is a SIGNED int whihc makes things a bit more complicated and I was told to do this shifting bits around, if this were a unsigned as you have there it would be that simple. ALmost there.....BTW you guys are fast, I didn't think I'd have an answer tonight so I watched 4 episodes of Gundam SEED *spectacular* and then Macross ZERO *absolutely amazing* ...wow the joys of downloading :)
0
 

Author Comment

by:javellan
ID: 8015498
Okay yes..Im sorry it needs to go into BINARY string.
Akshayxx could you provide a small explanation of the statements you used? I under stand what ? : are but if you coudl break it down that owuld be great, also this is a SIGNED int whihc makes things a bit more complicated and I was told to do this shifting bits around, if this were a unsigned as you have there it would be that simple. ALmost there.....BTW you guys are fast, I didn't think I'd have an answer tonight so I watched 4 episodes of Gundam SEED *spectacular* and then Macross ZERO *absolutely amazing* ...wow the joys of downloading :)
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8039807
while(i-->0){
       s[nBITS-i-1]=(x&(1<<i))?'1':'0';
}
s[nBITS]='\0';
}

lets  think the number is   100101101101
1 can be represented as     000000000001
so in my loop .. what i do is .. i shift 1 such that . its '1' bit moves to the nth position ..
then i take & with my number..
if i get 0 that means the bit at particular position is 0 .. so i put '0' in the array .. other wise i put '1'

see if u shift 000001 with 3 bits it will give
100101101101
000000001000
take the AND and that gives  
000000001000.. so at the last minus 3 index i shud store '1'
0
 
LVL 20

Expert Comment

by:jmcg
ID: 10018237
Nothing has happened on this question in over 10 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by akshayxx.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question