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 8924847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:31:42+00:00 2026-06-15T07:31:42+00:00

How can one specify a number as binary in gawk ? According to the

  • 0

How can one specify a number as binary in gawk?

According to the manual, gawk interprets all numbers as decimal unless they are preceded by a 0 (octal) or by a 0x (hexadecimal). Unlike in certain other languages, 0b does not do the trick.

For instance, the following lines do not give the desired output (010000 or 10000) because the values are interpreted as octal/decimal or decimal/decimal, respectively:

gawk '{print and(010000,110000)}'
0

gawk '{print and(10000,110000)}'
9488

I suspect that gawk may not support base-2 and that a user-defined function will be required to generate binary representations.

  • 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-06-15T07:31:43+00:00Added an answer on June 15, 2026 at 7:31 am

    You’re right, there’s no internal support for binary conversion in gawk. And incredibly, there isn’t even any in printf(). So you’re stuck with functions.

    Remember that awk is weakly typed. Which is why functions have insane behaviours like recognizing that “0x” at the beginning of a string means it’s a hexadecimal number. In a language like this, better to control your own types.

    Here’s a couple of functions I’ve had sitting around for years…

    #!/usr/local/bin/gawk -f
    
    function bin2dec(n) {
      result = 0;
      if (n~/[^01]/) {
        return n;
      }
      for (i=length(n); i; i--) {
        result += 2^(length(n)-i) * substr(n,i,1);
      }
      return result;
    }
    
    function dec2bin(n) {
      result = "";
      while (n) {
        if (n%2) {
          result = "1" result;
        } else {
          result = "0" result;
        }
        n = int(n/2);
      }
      return result;
    }
    
    {
      print dec2bin( and(bin2dec($1),bin2dec($2)) );
    }
    

    And the result:

    $ echo "1101 1011" | ./doit.awk
    1001
    $ echo "11110 10011" | ./doit.awk
    10010
    $ 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that in regex one can specify the desired number of matches via
For scikit-learn's KNN package , can one specify a pairwise distance metric (from the
In code one can specify globally accessible constants/enums/etc once that can then be reused
Using the CSS font-size property, one can specify the size as a percentage. What
I wonder if one can let the user specify an installation directory for an
One can easily use JDO syntax to query on multiple parameters as follows: //specify
When building Boost binary libraries with bjam , one may specify which compiler to
Can one place an arbitrary program (firefox, openoffice, etc...) in a QX11EmbedContainer? The fllowing
Can one of you smarties point me to some beginner content for converting an
Can one upload files to a domain other than the domain a script originates

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.