0

How can I write a program that reads an integer and displays a binary number without using loops, just with binary operators? (Only with basic functions)

 #include<stdio.h>     
 #include<stdint.h>     
 #include<math.h>        
 int main()       
 {uint8_t a;      
 scanf("%hhd", &a);   
 //i have read the integer, but I don't know how to go on  
 return 0;      
 }      
  • 1
    https://stackoverflow.com/questions/111928/is-there-a-printf-converter-to-print-in-binary-format – Sven Nilsson Oct 27 '20 at 01:12
  • To output the MSB: `putchar('0' + !!(a & 0x80))` – user3386109 Oct 27 '20 at 01:25
  • Does this answer your question? [Is there a printf converter to print in binary format?](https://stackoverflow.com/questions/111928/is-there-a-printf-converter-to-print-in-binary-format) – Marco Bonelli Oct 27 '20 at 01:28
  • By the way, the format "%hhd" is pretty bad. It might be understood by C as `%hd` which is `short int` but your variable is `uint8_t`. The value written on the stack will be `short int` which is probably 16 bits. You might want to use simple `unsigned int` with `%u`. If you want to keep with `uint8_t` you could refer to another question https://stackoverflow.com/questions/23748257/scanf-cant-scan-into-uint8-t – Robert Oct 27 '20 at 03:03
  • @Robert: what's the trouble with `%hhd`? Granted, it would be better as `%hhu` since the variable is `uint8_t` (an unsigned type), but C11 [§7.21.6.2 The `fscanf` function](http://port70.net/~nsz/c/c11/n1570.html#7.21.6.1) specifies the `hh` modifier (and so did C99 before it). I suppose that you could argue for the use of `` and `SCNu8` as the format: `"%" SCNu8`. – Jonathan Leffler Oct 27 '20 at 03:57
  • I met an issue with `%hhd` and `uint8_t` when running the second example in my answer. On my computer, `scanf` both write the variables `uint8_t a` and `Mask` I expect `%hhd` to be understood as `uint16_t`. Therefore, `scanf` writes the `uint8_t a` and a part of `Mask`. For that reason, I changed the type to `int`. – Robert Oct 27 '20 at 04:46

1 Answers1

0

Displays a binary number without using loops, just with binary operators:

#include<stdio.h>     
#include<stdint.h>     
#include<math.h>        
int main()       
{
  int a;      
  scanf("%u", &a);

  printf("Number: 0x%X\n", a);
  
  printf("%d", ((a & 0x80) >> 7));
  printf("%d", ((a & 0x40) >> 6));
  printf("%d", ((a & 0x20) >> 5));
  printf("%d", ((a & 0x10) >> 4));
  printf("%d", ((a & 0x08) >> 3));
  printf("%d", ((a & 0x04) >> 2));
  printf("%d", ((a & 0x02) >> 1));
  printf("%d", ((a & 0x01) >> 0));

  printf("\n");
  
  return 0;      
}      

Be careful, as the input is integer, I added a printf

131
Number: 0x83
10000011

Of course, it would be easier with loops :

int main()       
{
  int     a;
  uint8_t Mask;
  uint8_t Shift;
  scanf("%u", &a);

  printf("Number: %X\n", a);

  Mask  = 0x80;
  Shift = 7;
  
  while (Mask > 0)
  {
    printf("%d", ((a & Mask) >> Shift));
    Mask = Mask >> 1;
    Shift--;
  }

  printf("\n");
  
  return 0;      
}      
Robert
  • 2,711
  • 7
  • 15