Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print a binary value(1010) into decimal value(10) in GDB?

Tags:

c

linux

unix

gdb

I want to print the decimal value of 1010 in gdb, but it prints the result as it is what I gave last.

(gdb) 
(gdb) p/d 1010
$1 = 1010
(gdb)
like image 406
Kirubakaran Avatar asked Jun 21 '16 19:06

Kirubakaran


People also ask

What is the equivalent decimal value of binary number 1011 10?

Hence, The decimal equivalent of the given binary number is 11.


1 Answers

GDB's p[rint] command prints the value of the expression you provide, which is interpreted in the source language of the program being debugged. In C, your 1010 is a decimal literal, not a binary literal, so your basic problem is that you are giving GDB bad input.

Standard C has no support for binary literals, but GNU C supports them as an extension. The format is a binary digit string preceded by 0b or 0B, which you'll probably recognize as analogous to the standard format for hexadecimal literals. GDB recognizes this form.

Since print's default output radix for numbers is decimal, you don't need to specify an output format. Just use the command

p 0b1010
like image 185
John Bollinger Avatar answered Nov 15 '22 00:11

John Bollinger