How to print in binary

Sdílet
Vložit
  • čas přidán 8. 07. 2024
  • Printing in binary format using the printf function is not as easy as it is with other bases (decimal, hexadecimal or octal) but can be done relatively easy with this algorithm.
    Check out our Discord server: / discord

Komentáře • 33

  • @eulerco.2036
    @eulerco.2036 Před 3 lety +9

    did you count your bits ...>>9 ??. maybe you better should shift from 7 to 0

    • @CodeVault
      @CodeVault  Před 3 lety +7

      Oh yea, now I see the mistake. It should be:
      for (j = 7; j >= 0; j--)
      That's why we're getting 9 bits per byte at the end. Thanks for catching that!

  • @rikkoo
    @rikkoo Před rokem +5

    I love how the people who don't make coding videos are the first to correct other people's mistakes.
    Great tutorial, thanks

    • @CodeVault
      @CodeVault  Před rokem +3

      I still think it's valuable feedback. In a lot of the videos I omit certain aspects to make things easier to explain and understand (and possibly revisit later on). Sometimes it's good to have someone explain those aspects I omit in more detail in the comments below and, if the explanation is good, I end up pinning the comment

    • @danielesquivel3155
      @danielesquivel3155 Před rokem +1

      @@CodeVault yes

  • @gimB83
    @gimB83 Před 3 lety +2

    Thanks a lot for this well explained guide - that's exactly what I needed.

  • @manzoorelahitamjeed
    @manzoorelahitamjeed Před 3 lety +2

    Great video. Helped me a lot.

  • @mitchanx1337
    @mitchanx1337 Před 3 lety +2

    big thanks. that was really helpful with my project

  • @javierbravogutierrez5083

    Man, you are awesome!! Million thanks

  • @harshdubey5763
    @harshdubey5763 Před 3 lety +4

    Don't you think this is going a bit overboard? You can simply right shift and use %2 operation to get the binary. Then store it in an array and print it. Regardless, your videos are very helpful. Thanks.

    • @CodeVault
      @CodeVault  Před 3 lety +1

      True. Although I didn't want to use any additional memory. You can share the code for that idea here and I'll pin it if you want. Your idea is very good!

    • @harshdubey5763
      @harshdubey5763 Před 3 lety +1

      @@CodeVault Also, shouldn't "j" in the 2nd for loop be set to 7? If you set it to 8 then you have shifted the character 8 places and basically lost the MSB. The code will work for smaller numbers but try to test it with 128.

    • @CodeVault
      @CodeVault  Před 3 lety +2

      Yeah, that's a mistake I made, my bad. I pinned a comment with the correct version of the for loop

  • @tempusFugit1337
    @tempusFugit1337 Před 3 lety +2

    Hi, very useful Vid but there is an additional mistake in it. The outer for loop has to be " for(i = (sizeof(int) - 1); i >= 0; i--){} ". Elsewise he will print out the bits of single bytes in the right order but the whole bytes mirrored. By the way this code also works fine for negative integers as long as the hardware system uses internal two's complement notation (which is the case in 99,9%). Greetings

  • @mrjadhav1
    @mrjadhav1 Před rokem

    Just keep two loops as it, and keep number untouched, AND with 1

  • @AnalogDude_
    @AnalogDude_ Před 5 měsíci

    Cool video

  • @Ca3yMiX
    @Ca3yMiX Před 5 lety +2

    thanks for the Video, can u make a other one for Reverse Bits please.

    • @CodeVault
      @CodeVault  Před 5 lety

      Sure thing. Thanks for the suggestion!

  • @jamesbela9719
    @jamesbela9719 Před 2 lety

    Thanks for the video but what if it is float or double numbers ?

    • @CodeVault
      @CodeVault  Před 2 lety

      You can use the same exact technique

  • @sepgh2216
    @sepgh2216 Před 4 měsíci

    Great explanation. Some questions though. According to this, the representation of '-13' is: '11110011 11111111 11111111 11111111'. A) is it correct to assume that the first bit in first byte is for the signature? 1111001>1

    • @sepgh2216
      @sepgh2216 Před 4 měsíci

      update: I checked this video about signed number binary representation and its all starting to make sense: czcams.com/video/-CEJXDeDsAQ/video.htmlsi=OeG2PcYKywU-uU9q&t=692

  • @muhammadchaudhry4078
    @muhammadchaudhry4078 Před 2 měsíci

    Hey Guys!
    While getting execution of printf command with%hhd to display the binary characters in Visual Studio Code, it only gives result in decimal like 13 and not in binary. Can someone help to get the result in binary bytes for integers, as shown in the video..
    Thanks

    • @CodeVault
      @CodeVault  Před 2 měsíci +1

      %hhd is decimal and I use it to print the 0s and 1s from a char data type (make sure that's what you are passing to printf). Otherwise you can use %x to print in hex for larger data types

  • @emirabouzaki8001
    @emirabouzaki8001 Před rokem

    i get a segfault when trying this, any advice?

    • @emirabouzaki8001
      @emirabouzaki8001 Před rokem

      i'm trying to do it for the double datatype

    • @CodeVault
      @CodeVault  Před rokem

      You can send the code either here or on discord.code-vault.net

  • @ishangupta8731
    @ishangupta8731 Před 2 lety +2

    #include
    #include
    #include
    /**
    * size of datatype (number of bytes)
    * typically the output of sizeof operator
    * pointer to 0th byte
    * in simple terms byte array
    */
    void print_binary(size_t size, void* ip_data){
    char *data = (char*) ip_data;
    long long i, j;
    for(i=0; i=0; j--){
    char bit = (byte >> j) & 1;
    printf("%hhd", bit);
    }
    printf(" ");
    }
    }
    int main(int argc, char const *argv[])
    {
    int a = 13;
    print_binary(sizeof(int), &a);
    printf("
    ");
    return 0;
    }

    • @ishangupta8731
      @ishangupta8731 Před 2 lety +1

      @CodeVault is this solution to your question correct?

    • @CodeVault
      @CodeVault  Před 2 lety +2

      Yep, that is correct. Very good job!

    • @ishangupta8731
      @ishangupta8731 Před 2 lety +1

      @@CodeVault You are great teacher. Keep up the good work 😃