Binary!!! How do I manage that?

This is a discussion on Binary!!! How do I manage that? within the C Programming forums, part of the General Programming Boards category; Hey yall! I had a few quesitons about binary here... - How do I store binary, as an int? like; ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    14

    Binary!!! How do I manage that?

    Hey yall!

    I had a few quesitons about binary here...

    - 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. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote 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;
    Quote 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;
    Quote 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 ) { /* ... */ }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    14
    can you explain the last one a bit better???

    I'm a perl guy lol

    thank you

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote 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.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could have just told him to read the FAQ.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote 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.

    Edit: Added "bit 5" clarification.
    Last edited by Kennedy; 01-19-2007 at 10:52 PM.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote 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.

    Edit: Added "bit 5" clarification.
    No. You're right. But I've been out of my mind longer!
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    #include<stdio.h>
    
    int main()
    {
        int a = 0x515;  /*  010100010101 */
        int mask = 0x80; /* 000010000000 */
                         /* ------------ */
                         /* 000000000000 */
        
        if(a & mask)
             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
    Last edited by ssharish2005; 01-20-2007 at 09:31 PM.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    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.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote 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"?
    Quote Originally Posted by ssharish2005
    hope this will help.
    No it will help.
    Yes it will help.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    You pointed out. Thats was my mistake. Should have been proper sentence

    ssharish2005

    Code Edited

  12. #12
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Quote Originally Posted by Kennedy
    (stinkin boot loader. . . )
    Hehe.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  13. #13
    Registered User
    Join Date
    Jan 2007
    Posts
    14
    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);
    	}
    }

  14. #14
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    1. int main
    2. better to use unsigned int for >>
    3. count is not initialized and not used after ++
    4. code alignment
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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.
    My best code is written with the delete key.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays vs lists? And containers in general!
    By clegs in forum C++ Programming
    Replies: 22
    Last Post: 12-03-2007, 02:02 PM
  2. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

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