-3

Given int a=0x1234 print 4321 using bitwise operations,I did like this is there any better way of doing this.

#include<bits/stdc++.h>
using namespace std;
int main(){
    int val = 0x1234;
    int mask = 0x000F;
    ///trying to print 4
    int ans = val&mask;
    cout<<ans<<endl;
    val=val>>4;
    ans = val&mask;
    cout<<ans<<endl;
    val=val>>4;
    ans=val&mask;
    cout<<ans<<endl;

}
user10891599
  • 41
  • 1
  • 5

3 Answers3

1

I think, this certainly looks better than yours:

#include <iostream>
int main() {
    unsigned val = 0x12AB;
    std::cout << std::hex << std::uppercase;
    do std::cout << (val & 0xF) << '\n';
    while (val >>= 4);
    // std::cout << std::nouppercase << std::dec;
}

Prints:

B
A
2
1


To print in the characters in lowercase, just remove std::uppercase.
To print A as 10, B as 11 ..., completely remove line 4.
Uncomment line 7 if you intend to print other numbers in your code in regular format.

UPDATE : Switched to do-while from while to handle val = 0.

brc-dd
  • 10,788
  • 3
  • 47
  • 67
-1

This works for me:

#include <stdio.h>

int main()
{
    static char hex[] = "0123456789abcdef";
    int a = 0x1234abd;

    while( a > 0 ) {
        printf( "%c", hex[a & 0xf] );
        a >>= 4;
    }
}

output:

$ ./a.out 
dba4321

this is an answer to the comment from brc-dd -- if I was writing this code from the scratch, I would output the number to the string using:

std::stringstream stream;
stream << std::hex << int_value;
std::string hex( stream.str() );

And then reverse the string using:

string reversed(hex.rbegin(), hex.rend());

But OP wanted to use bitwise ops....

lenik
  • 23,228
  • 4
  • 34
  • 43
  • @lenik Why not just use `%x` or `%X` instead of `%c` and replace `hex[a & 0xf]` with just `a & 0xf`? No need of the `hex` array. – brc-dd Jul 19 '20 at 08:21
  • can you tell me the meaning of a & 0xf? – user10891599 Jul 19 '20 at 08:24
  • @user10891599 Firstly, you had used the same logic in your code. Did it just came to your mind and you used it even without knowing it?!! Secondly, the answer to your question : it performs bitwise AND operation of `a` and `0xf`. In this case, the operation is giving us the last digit of hex representation of `a`. – brc-dd Jul 19 '20 at 08:28
  • @brc-dd answered in the answer. – lenik Jul 19 '20 at 08:39
  • @lenik I think you misinterpreted my comment. I never said to write from scratch or without using bitwise operations. I was just suggesting to use `%x` as format specifier in `printf`. – brc-dd Jul 19 '20 at 08:41
  • @brc-dd please, read the question title: "Extract the hex number digits using bitwise operators" -- using `printf` formats does not do exactly that. – lenik Jul 19 '20 at 08:43
  • @lenik You are still not understanding. See [this code](https://wandbox.org/permlink/8PI0ojDYMv8Hwg40). – brc-dd Jul 19 '20 at 08:44
-3

I think you should just declare 1 more variable and initialize it to Zero and then multiply ans with pow(16,i++) each time you print or calculate bits value.

Please try below code.. I compiled and run it with g++.

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int val = 0x1234;
    int mask = 0x000F;
    ///trying to print 4
    int i = 0;
    int ans = val & mask;
    cout << ans * pow(16, i++) << endl;
    val = val >> 4;
    ans = val & mask;
    cout << ans * pow(16, i++) << endl;
    val = val >> 4;
    ans = val & mask;
    cout << ans * pow(16, i++) << endl;
}
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
Ankit Mishra
  • 530
  • 8
  • 16