# Thread: Binary!!! How do I manage that?

1. ## Binary!!! How do I manage that?

Hey yall!

- How do I store binary, as an int?
like; int a = 100101010101; ???

- How do I shift bits?
like move this down 3 bits? 100111

- Check if bit 8 is 0 or 1...
like: 010100010101...?

THANK YOU PROS!

2. Originally Posted by expresspotato
- How do I store binary, as an int?
like; int a = 100101010101; ???
Use hex or octal notation. Ex:
Code:
`unsigned int a = 0x955;`
Originally Posted by expresspotato
- How do I shift bits?
like move this down 3 bits? 100111
With a bitshift operator (<< or >>). Ex:
Code:
`unsigned int b = a >> 3;`
Originally Posted by expresspotato
- Check if bit 8 is 0 or 1...
like: 010100010101...?
Use bitwise AND (&) with a mask. Ex:
Code:
`if ( a & 0x10 ) { /* ... */ }`

3. can you explain the last one a bit better???

I'm a perl guy lol

thank you

4. Originally Posted by Dave_Sinkula
Use bitwise AND (&) with a mask. Ex:
Code:
`if ( a & 0x10 ) { /* ... */ }`
Some value, bit 8. If it is set, do something. You could have an else for if it is not set.

5. You could have just told him to read the FAQ.

Quzah.

6. Originally Posted by Dave_Sinkula
Some value, bit 8. If it is set, do something. You could have an else for if it is not set.
Bit 8???? 0x10????? 0b00010000. . . bit 5 (0-offset folks would call this 4, but PCB makers refer to it as 5)????????? Or, am I missing something? I mean, I have been writing Assembly for the past three weeks (stinkin boot loader. . . ) but I don't think I'm that far out of my mind yet.

7. Originally Posted by Kennedy
Bit 8???? 0x10????? 0b00010000. . . bit 5 (0-offset folks would call this 4, but PCB makers refer to it as 5)????????? Or, am I missing something? I mean, I have been writing Assembly for the past three weeks (stinkin boot loader. . . ) but I don't think I'm that far out of my mind yet.

No. You're right. But I've been out of my mind longer!

8. Code:
```#include<stdio.h>

int main()
{
int a = 0x515;  /*  010100010101 */
int mask = 0x80; /* 000010000000 */
/* ------------ */
/* 000000000000 */

printf("Yes the 8th bit is 1\n");
else
printf("No the 8th bit is not 1\n");

getchar();
return 0;
}

/* my output
No the 8th bit is 1
*/```
hope this will help. This is just a sample code.

ssharish2005

9. Originally Posted by ssharish2005
Code:
```         printf("Yes the 8th bit is 1\n");

printf("No the 8th bit is 1\n");```
What's the difference between "yes the 8th bit is 1" and "no the 8th bit is 1"?
Originally Posted by ssharish2005
hope this will help.
No it will help.
Yes it will help.

Quzah.

10. You pointed out. Thats was my mistake. Should have been proper sentence

ssharish2005

Code Edited

11. Originally Posted by Kennedy
(stinkin boot loader. . . )
Hehe.

12. thanks! I'm not entirely getting this whole bitshifting thing, grrr....

Anyways, here is a code sniplet, how can I re-write this as a while loop? without needing to call bin (it self) everytmie?

Code:
```main (){
int a = 12;

clrscr();

bin(12);
}

void bin(int i){
int b=0, j=0,count;
count++;

if(i != 0){

b = i >>1;

bin(b);

j = i&0x01;

printf("%d",j);
}
}```

13. 1. int main
2. better to use unsigned int for >>
3. count is not initialized and not used after ++
4. code alignment

14. Let's start by fixing the glaring problems.

First, you need to include <stdio.h>. You would also need to include <conio.h> if I weren't recommending that you not use clrscr.

>main (){
Code:
```int main ( void )
{
return 0;
}```
Always. No excuses. Yes, I've heard them all before, and no, I'm not interested in hearing them again. Just use it.

>int a = 12;
You don't use this, and in the current code it's just filler anyway.

>clrscr();
This is a great way to ........ users off. I can guarantee that if I start a console program and it clears all of my previous output from other programs, I'll never use it again.

>count++;
You just invoked undefined behavior by accessing an indeterminate value from an uninitialized variable.

>how can I re-write this as a while loop?
If you don't mind the binary value in reverse, it's trivial:
Code:
```#include <stdio.h>

void bin ( int i )
{
while ( i != 0 ) {
printf ( "%d", i & 0x01 );
i = i >> 1;
}
}

int main ( void )
{
bin ( 12 );

return 0;
}```
If you do mind, you need to cache the results and then print them properly. Using an array, for example.