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
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
%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
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.
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!
@@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.
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
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
update: I checked this video about signed number binary representation and its all starting to make sense: th-cam.com/video/-CEJXDeDsAQ/w-d-xo.htmlsi=OeG2PcYKywU-uU9q&t=692
did you count your bits ...>>9 ??. maybe you better should shift from 7 to 0
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!
I love how the people who don't make coding videos are the first to correct other people's mistakes.
Great tutorial, thanks
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
@@CodeVault yes
Thanks a lot for this well explained guide - that's exactly what I needed.
Great video. Helped me a lot.
big thanks. that was really helpful with my project
Man, you are awesome!! Million thanks
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
%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
Just keep two loops as it, and keep number untouched, AND with 1
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.
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!
@@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.
Yeah, that's a mistake I made, my bad. I pinned a comment with the correct version of the for loop
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
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
update: I checked this video about signed number binary representation and its all starting to make sense: th-cam.com/video/-CEJXDeDsAQ/w-d-xo.htmlsi=OeG2PcYKywU-uU9q&t=692
thanks for the Video, can u make a other one for Reverse Bits please.
Sure thing. Thanks for the suggestion!
Thanks for the video but what if it is float or double numbers ?
You can use the same exact technique
Cool video
i get a segfault when trying this, any advice?
i'm trying to do it for the double datatype
You can send the code either here or on discord.code-vault.net
Big Endian
for (int bits = 0; bits < (sizeof(uint64_t) * 8); bits++) {
printf("%llu", 0x01ull & num);
num >>= 1;
}
Little endian
size_t bit_size = sizeof(num) * 8;
int shift_size= 1;
for (size_t i = 1ull
#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;
}
@CodeVault is this solution to your question correct?
Yep, that is correct. Very good job!
@@CodeVault You are great teacher. Keep up the good work 😃