Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5929337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:24:31+00:00 2026-05-22T14:24:31+00:00

#include<stdio.h> #include<iostream.h> main() { unsigned char c,i; union temp { float f; char c[4];

  • 0
#include<stdio.h>
#include<iostream.h>
main()
{
  unsigned char c,i;
  union temp
  {
    float f;
    char c[4];
  } k;
  cin>>k.f;
  c=128;
  for(i=0;i<8;i++)
  {
    if(k.c[3] & c) cout<<'1';
    else cout<<'0';
    c=c>>1;
  }
  c=128;
  cout<<'\n';
  for(i=0;i<8;i++)
  {
    if(k.c[2] & c) cout<<'1';
    else cout<<'0';
    c=c>>1;
  }
  return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:24:32+00:00Added an answer on May 22, 2026 at 2:24 pm

    Bitwise Operation in your code

    c = 128 therefore the binary representation is

    c = 10000000
    

    a & c will and every ith but if c with evert ith bit of a. Because c only has 1 in the MSB position (pos 7), so a & c will be non-zero if a has a 1 in its position 7 bit, if a has a 0 in pos bit, then a & c will be zero. This logic is used in the if block above. The if block is entered depending upon if the MSB (position 7 bit) of the byte is 1 or not.

    Suppose a = ? ? ? ? ? ? ? ? where a ? is either 0 or 1
    Then

    a = ? ? ? ? ? ? ? ? 
    AND & & & & & & & &
    c = 1 0 0 0 0 0 0 0
        ---------------
        ? 0 0 0 0 0 0 0
    

    As 0 & ? = 0. So if the bit position 7 is 0 then answer is 0 is bit position 7 is 1 then answer is 1.

    In each iteration c is shifted left one position, so the 1 in the c propagates left wise. So in each iteration masking with the other variable you are able to know if there is a 1 or a 0 at that position of the variable.

    Use in your code

    You have

    union temp
    {
      float f;
      char c[4];
    } k;
    

    Inside the union the float and the char c[4] share the same memory location (as the property of union).
    Now, sizeof (f) = 4bytes) You assign k.f = 5345341 or whatever . When you access the array k.arr[0] it will access the 0th byte of the float f, when you do k.arr[1] it access the 1st byte of the float f . The array is not empty as both the float and the array points the same memory location but access differently. This is actually a mechanism to access the 4 bytes of float bytewise.
    NOTE THAT k.arr[0] may address the last byte instead of 1st byte (as told above), this depends on the byte ordering of storage in memory (See little endian and big endian byte ordering for this)

                   Union k
    +--------+--------+--------+--------+   --+
    | arr[0] | arr[1] | arr[2] | arr[3] |     |
    +--------+--------+--------+--------+     |---> Shares same location (in little endian)
    |              float f              |     |
    +-----------------------------------+   --+
    

    Or the byte ordering could be reversed

                   Union k
    +--------+--------+--------+--------+   --+
    | arr[3] | arr[2] | arr[1] | arr[0] |     |
    +--------+--------+--------+--------+     |---> Shares same location (in big endian)
    |              float f              |     |
    +-----------------------------------+   --+
    

    Your code loops on this and shifts the c which propagates the only 1 in the c from bit 7 to bit 0 in one step at a time in each location, and the bitwise anding checks actually every bit position of the bytes of the float variable f, and prints a 1 if it is 1 else 0.

    If you print all the 4 bytes of the float, then you can see the IEEE 754 representation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <stdio.h> int main() { float a = 1234.5f; printf(%d\n, a); return 0; }
#include stdio.h #include conio.h #include <iostream> using namespace std; int main (void) { char
#include <stdio.h> #include <iostream> using namespace std; float cost, total; bool loop(char item){ switch
#include <stdio.h> int main() { unsigned long long int num = 285212672; //FYI: fits
#include<stdio.h> #include<time.h> int main() { clock_t start; double d; long int n,i,j; scanf(%ld,&n); n=100000;
#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { // int char str[40],ch; FILE*fp,*fp1,*fp2; fp=fopen(ide_input,w); fp1=fopen(error_log,w); fp2=fopen(lex_output,w);
#include <stdlib.h> #include <stdio.h> #include <iostream> #include <string.h> using namespace std; int main() {
#include <stdio.h> #include <string.h> #include <conio.h> #include <iostream> using namespace std; char a[21]; //
#include<stdio.h> #include<conio.h> #include<iostream.h> void main() { int wh=1,i,j; int sale[5][3]; clrscr(); for(i=1;i<=5;i++) { for(j=1;j<=3;j++)
#include <stdio.h> #include <iostream> using namespace std; int main(void) { bool premiereLignefaite = false;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.