How to extract operand size in bits of a C program?


 
Thread Tools Search this Thread
Top Forums Programming How to extract operand size in bits of a C program?
# 1  
Old 08-08-2016
How to extract operand size in bits of a C program?

Hi I'm new to shell programming. How do I extract the size of an operand in a simple instruction in a C program? Lets say there is an addition a+b somewhere in a C code where a and b are integers. How to extract the size of a & b in bits? Is there something called intermediate code before an executable code which shows these details?
# 2  
Old 08-08-2016
You don't find the number of bits in the result of adding two values in a C program in the shell; you find that number in your C program. And, the number of bytes in the result of an addition depends on the sizes of the integers you are adding. Due to integral value promotion rules in C on a UNIX-, Linux-, or BSD-based operating system, the number of bits in the result of adding a and b will be the number of bits in the largest of the two values a and b. The number of bytes in a can be found with sizeof(a); and the number of bytes in b can be found with sizeof(b); and the number of bits in the result will be 8 times the maximum of those two sizes.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 08-08-2016
I thought of a method to find out exact no. of bits in a operand. Right shifting by 1 bit until it becomes zero. No. of times shifted will be size of the operand. But how do I write a shell script to do this?
# 4  
Old 08-08-2016
OK. That is a different question totally unrelated to the title of this thread. The highest bit set in a non-negative integer number for a number that will fit in a C signed long integer value can be found in a shell that conforms to the POSIX standards using something like:
Code:
printf 'Enter non-negative integer (ctl-D to exit): '
while read -r n
do	printf 'Highest bit set in %s is ' "$n"
	bits=0
	while [ "$n" -gt 0 ]
	do	bits=$((bits + 1))
		n=$((n / 2))
	done
	printf '%d.\n' $bits
	printf 'Enter another non-negative integer (ctl-D to exit): '
done
printf '\nGoodbye\n'

Of course, this doesn't do any validity checking on the values entered and may die a horrible death if a non-numeric entry or an out-of-range numeric entry is given. With a 1993 or later version of the Korn shell, you might be able to process numbers considerably larger than 2**63 - 1.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 08-08-2016
Thank you very much for helping me who had no idea of doing this Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do I determine the best number to use for the bs (block size) operand of the dd command?

When I create a bootable Linux distro installation USB drive, I use this command: sudo dd if=/Path/to/linux_distro.iso of=/dev/rdisk<disk number> bs=<number of bytes> When I look it up, I've seen variations of people choosing 4M, and I think 8M, 2M, and maybe even 1M. If I leave the operand... (4 Replies)
Discussion started by: Quenz
4 Replies

2. Programming

Size of memory used by a program

Hello, Here is a portion of my code: a=(int *) malloc(dim*dim*sizeof(int)); b=(int *) malloc(dim*dim*sizeof(int)); c=(int *) malloc(dim*dim*sizeof(int)); for(i=0;i<dim;i++) for(j=0;j<dim;j++) c= rand(); for(i=0;i<dim;i++) for(j=0;j<dim;j++) b=rand(); ... (6 Replies)
Discussion started by: chercheur111
6 Replies

3. UNIX Desktop Questions & Answers

Knowing the size and location of variables in a C program

So I need some help with this. Pardon me if I'm posting in the wrong forum, after some googling for my answer and finding nothing I found this forum. It seemed appropriate for what I was seeking. I just didnt find a forum that concerned the use of GDB. I'm learning to use the C language and GDB.... (2 Replies)
Discussion started by: Cambria
2 Replies

4. What is on Your Mind?

Place Bits & Win Bits!!! - 17th Annual Satellite Awards

Ten movies have been nominated as best motion picture by the International Press Academy, presentation of the 2012 Satellite Awards will be held on 16th December at Los Angeles, CA. Place your bits here on one of the below nominated movie of your choice:- Argo ... (0 Replies)
Discussion started by: Yoda
0 Replies

5. Programming

Compiling a 64 bits program using gcc

Hi Everyone, I can ask what is the option to compile a 64 bits program using gcc. I have looked everywhere but can't find it. Before I used to use cc and the -q64 flag was the option to generate the 64 bits binary. Can anyone tell me what is the flags when using gcc. Thanks...... (3 Replies)
Discussion started by: arizah
3 Replies

6. UNIX for Dummies Questions & Answers

32 bits procesaor with 64 bits Solaris

people i have a problem i have a 32 bits sparc processor, and solaris 64 bits processor, i install a oracle data base 64 bits, but my oracle will not run because my processor is from 32 bits this is ok??, i know if i have x86 i cannot install a 64 bits operatin system in a 32 bits processor. ... (0 Replies)
Discussion started by: enkei17
0 Replies

7. Solaris

How to know the size of the program currently executing in memory

hey everybody, i am currently working on solaris 10 os on a m5000 server. my problem is when i want the exact size of a program in execution, i am unable to do it. earlier i thought the RSS field of prstat but because of its large size it cant be the size. pmap -x shows some output but it includes... (2 Replies)
Discussion started by: aryansheikh
2 Replies

8. UNIX for Dummies Questions & Answers

Changing 24 bits to 8 bits display

Hello all, I was wondering if anyone can tell me how to change 24 bits depth display to 8 bits depth display for Sun Ultra1, running Solaris 8? THANKS in advance. I think that the command is ffbconfig, but it has nothing about depth. (4 Replies)
Discussion started by: larry
4 Replies
Login or Register to Ask a Question