LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-23-2013, 11:12 PM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Rep: Reputation: 76
Accessing the low and high bytes of a short integer (C)?


Hi:
Is there a way to access the low and high bytes of a short integer in C? I am doing
Code:
unsigned short chk;
unsigned char* _chk= &chk;

chk=0x1234;
printf("High byte= %x low byte= %x\n", _chk[0], _chk[1]);
But there must be an easier way to do it. Something like low(chk), high(chk). It's a question.
 
Old 08-23-2013, 11:24 PM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

Code:
#define LOW(x) (x&0xFF)
#define HIG(x) LOW((x>>8))
 
Old 08-23-2013, 11:31 PM   #3
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Quote:
Originally Posted by firstfire View Post
Hi.

Code:
#define LOW(x) (x&0xFF)
#define HIG(x) LOW((x>>8))
I don't get it. If x=0x1234, then LOW = 34 and HIG = 00.
 
Old 08-23-2013, 11:48 PM   #4
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Now I see. But is not #define HIG(x) x>>8 enough?
 
Old 08-24-2013, 12:02 AM   #5
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Quote:
Originally Posted by stf92 View Post
Now I see. But is not #define HIG(x) x>>8 enough?
Imagine that you have 4-byte number as input.
EDIT: And never omit parentheses in macros! What if you wanted to compute HIG(x+1)?

Last edited by firstfire; 08-24-2013 at 12:09 AM.
 
1 members found this post helpful.
Old 08-24-2013, 01:05 AM   #6
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Yes, that is a thing I learned time ago. But I insist: if the number is
Code:
unsigned short x;
then, if x = 0x1234, x >> 8 should give me 0x12. Now if it declared as integer, well, integer could be 4 or 8 bytes, I see the point here. Thanks for your reply.
 
Old 08-24-2013, 01:41 AM   #7
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Why not just use set width types in stdint.h. I tend to do that now for everything but counters (unsigned int or long).
 
Old 08-24-2013, 01:46 AM   #8
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Quote:
Originally Posted by H_TeXMeX_H View Post
Why not just use set width types in stdint.h. I tend to do that now for everything but counters (unsigned int or long).
How do you use it?
 
Old 08-24-2013, 03:20 AM   #9
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Code:
#include <stdio.h>
#include <stdint.h>

#define LOW(x) ((x) & 0xff)
#define HIGH(x) ((x) >> 8)

int main (void)
{
	uint16_t chk = 0x1234;

	printf ("%02x %02x\n", HIGH (chk), LOW (chk));

	return 0;
}
 
Old 08-24-2013, 03:23 AM   #10
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Thank you very much, TeXMeX.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] High CPU load, but low CPU usage (high idle CPU) baffy Linux - Newbie 5 03-13-2013 09:24 AM
High Load Average, Low CPU, Low IO Wait ordaolmayanadam Linux - Server 13 07-15-2010 06:55 AM
Adding high and low bytes of a short george_mercury Programming 4 12-06-2004 07:11 PM
high and low port bytes [cacheflow] Linux - Networking 0 01-30-2002 12:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration